diff --git a/.changes/unreleased/Fixes-20240823-123108.yaml b/.changes/unreleased/Fixes-20240823-123108.yaml new file mode 100644 index 0000000000..ceac1e2bdc --- /dev/null +++ b/.changes/unreleased/Fixes-20240823-123108.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Fixes bug where conversion metric query fails when filter with base semantic model's dimension is provided +time: 2024-08-23T12:31:08.257686-04:00 +custom: + Author: WilliamDee + Issue: "1210" diff --git a/metricflow-semantics/metricflow_semantics/specs/measure_spec.py b/metricflow-semantics/metricflow_semantics/specs/measure_spec.py index 0262d72685..8c4994a2f1 100644 --- a/metricflow-semantics/metricflow_semantics/specs/measure_spec.py +++ b/metricflow-semantics/metricflow_semantics/specs/measure_spec.py @@ -77,7 +77,11 @@ def post_aggregation_spec(self) -> MeasureSpec: fill_nulls_with=self.fill_nulls_with, ) else: - return self.measure_spec + return MeasureSpec( + element_name=self.measure_spec.element_name, + non_additive_dimension_spec=self.measure_spec.non_additive_dimension_spec, + fill_nulls_with=self.fill_nulls_with, + ) @dataclass(frozen=True) diff --git a/metricflow/dataflow/builder/dataflow_plan_builder.py b/metricflow/dataflow/builder/dataflow_plan_builder.py index 856384028c..81663bd69b 100644 --- a/metricflow/dataflow/builder/dataflow_plan_builder.py +++ b/metricflow/dataflow/builder/dataflow_plan_builder.py @@ -258,7 +258,7 @@ def _build_aggregated_conversion_node( ) # Build measure recipes - base_required_linkable_specs, _ = self.__get_required_and_extraneous_linkable_specs( + base_required_linkable_specs, extraneous_linkable_specs = self.__get_required_and_extraneous_linkable_specs( queried_linkable_specs=queried_linkable_specs, filter_specs=base_measure_spec.filter_spec_set.all_filter_specs, ) @@ -333,6 +333,11 @@ def _build_aggregated_conversion_node( unaggregated_base_measure_node = JoinOnEntitiesNode.create( left_node=unaggregated_base_measure_node, join_targets=base_measure_recipe.join_targets ) + if len(base_measure_spec.filter_spec_set.all_filter_specs) > 0: + unaggregated_base_measure_node = WhereConstraintNode.create( + parent_node=unaggregated_base_measure_node, + where_specs=base_measure_spec.filter_spec_set.all_filter_specs, + ) filtered_unaggregated_base_node = FilterElementsNode.create( parent_node=unaggregated_base_measure_node, include_specs=group_specs_by_type(required_local_specs) @@ -359,8 +364,8 @@ def _build_aggregated_conversion_node( # Aggregate the conversion events with the JoinConversionEventsNode as the source node recipe_with_join_conversion_source_node = SourceNodeRecipe( source_node=join_conversion_node, - required_local_linkable_specs=base_measure_recipe.required_local_linkable_specs, - join_linkable_instances_recipes=base_measure_recipe.join_linkable_instances_recipes, + required_local_linkable_specs=queried_linkable_specs.as_tuple, + join_linkable_instances_recipes=(), ) # TODO: Refine conversion metric configuration to fit into the standard dataflow plan building model # In this case we override the measure recipe, which currently results in us bypassing predicate pushdown diff --git a/tests_metricflow/integration/query_output/test_conversion_metrics.py b/tests_metricflow/integration/query_output/test_conversion_metrics.py new file mode 100644 index 0000000000..3acbce2d36 --- /dev/null +++ b/tests_metricflow/integration/query_output/test_conversion_metrics.py @@ -0,0 +1,178 @@ +from __future__ import annotations + +import datetime + +import pytest +from _pytest.fixtures import FixtureRequest +from metricflow_semantics.test_helpers.config_helpers import MetricFlowTestConfiguration + +from metricflow.engine.metricflow_engine import MetricFlowQueryRequest +from metricflow.protocols.sql_client import SqlClient +from tests_metricflow.integration.conftest import IntegrationTestHelpers +from tests_metricflow.snapshot_utils import assert_str_snapshot_equal + + +@pytest.mark.sql_engine_snapshot +def test_conversion_metric( + request: FixtureRequest, + mf_test_configuration: MetricFlowTestConfiguration, + sql_client: SqlClient, + it_helpers: IntegrationTestHelpers, +) -> None: + """Test query against a conversion metric.""" + query_result = it_helpers.mf_engine.query( + MetricFlowQueryRequest.create_with_random_request_id( + metric_names=("visit_buy_conversion_rate",), + group_by_names=("metric_time",), + order_by_names=("metric_time",), + ) + ) + assert query_result.result_df is not None, "Unexpected empty result." + + assert_str_snapshot_equal( + request=request, + mf_test_configuration=mf_test_configuration, + snapshot_id="query_output", + snapshot_str=query_result.result_df.text_format(), + sql_engine=sql_client.sql_engine_type, + ) + + +@pytest.mark.sql_engine_snapshot +def test_conversion_metric_with_window( + request: FixtureRequest, + mf_test_configuration: MetricFlowTestConfiguration, + sql_client: SqlClient, + it_helpers: IntegrationTestHelpers, +) -> None: + """Test query against a conversion metric with a window.""" + query_result = it_helpers.mf_engine.query( + MetricFlowQueryRequest.create_with_random_request_id( + metric_names=("visit_buy_conversion_rate_7days",), + group_by_names=("metric_time",), + order_by_names=("metric_time",), + ) + ) + assert query_result.result_df is not None, "Unexpected empty result." + + assert_str_snapshot_equal( + request=request, + mf_test_configuration=mf_test_configuration, + snapshot_id="query_output", + snapshot_str=query_result.result_df.text_format(), + sql_engine=sql_client.sql_engine_type, + ) + + +@pytest.mark.sql_engine_snapshot +def test_conversion_metric_with_categorical_filter( + request: FixtureRequest, + mf_test_configuration: MetricFlowTestConfiguration, + sql_client: SqlClient, + it_helpers: IntegrationTestHelpers, +) -> None: + """Test query against a conversion metric with a categorical filter.""" + query_result = it_helpers.mf_engine.query( + MetricFlowQueryRequest.create_with_random_request_id( + metric_names=("visit_buy_conversion_rate",), + group_by_names=("metric_time", "visit__referrer_id"), + order_by_names=("metric_time", "visit__referrer_id"), + where_constraints=("{{ Dimension('visit__referrer_id') }} = 'fb_ad_1'",), + ) + ) + assert query_result.result_df is not None, "Unexpected empty result." + + assert_str_snapshot_equal( + request=request, + mf_test_configuration=mf_test_configuration, + snapshot_id="query_output", + snapshot_str=query_result.result_df.text_format(), + sql_engine=sql_client.sql_engine_type, + ) + + +@pytest.mark.sql_engine_snapshot +def test_conversion_metric_with_time_constraint( + request: FixtureRequest, + mf_test_configuration: MetricFlowTestConfiguration, + sql_client: SqlClient, + it_helpers: IntegrationTestHelpers, +) -> None: + """Test query against a conversion metric with a time constraint and categorical filter.""" + query_result = it_helpers.mf_engine.query( + MetricFlowQueryRequest.create_with_random_request_id( + metric_names=("visit_buy_conversion_rate",), + group_by_names=("visit__referrer_id",), + order_by_names=("visit__referrer_id",), + where_constraints=("{{ Dimension('visit__referrer_id') }} = 'fb_ad_1'",), + time_constraint_start=datetime.datetime(2020, 1, 1), + time_constraint_end=datetime.datetime(2020, 1, 2), + ) + ) + assert query_result.result_df is not None, "Unexpected empty result." + + assert_str_snapshot_equal( + request=request, + mf_test_configuration=mf_test_configuration, + snapshot_id="query_output", + snapshot_str=query_result.result_df.text_format(), + sql_engine=sql_client.sql_engine_type, + ) + + +@pytest.mark.sql_engine_snapshot +def test_conversion_metric_with_window_and_time_constraint( + request: FixtureRequest, + mf_test_configuration: MetricFlowTestConfiguration, + sql_client: SqlClient, + it_helpers: IntegrationTestHelpers, +) -> None: + """Test query against a conversion metric with a window, time constraint, and categorical filter.""" + query_result = it_helpers.mf_engine.query( + MetricFlowQueryRequest.create_with_random_request_id( + metric_names=("visit_buy_conversion_rate_7days",), + group_by_names=( + "metric_time", + "visit__referrer_id", + ), + order_by_names=("metric_time", "visit__referrer_id"), + where_constraints=("{{ Dimension('visit__referrer_id') }} = 'fb_ad_1'",), + time_constraint_start=datetime.datetime(2020, 1, 1), + time_constraint_end=datetime.datetime(2020, 1, 2), + ) + ) + assert query_result.result_df is not None, "Unexpected empty result." + + assert_str_snapshot_equal( + request=request, + mf_test_configuration=mf_test_configuration, + snapshot_id="query_output", + snapshot_str=query_result.result_df.text_format(), + sql_engine=sql_client.sql_engine_type, + ) + + +@pytest.mark.sql_engine_snapshot +def test_conversion_metric_with_filter_not_in_group_by( + request: FixtureRequest, + mf_test_configuration: MetricFlowTestConfiguration, + sql_client: SqlClient, + it_helpers: IntegrationTestHelpers, +) -> None: + """Test query against a conversion metric with a filter that doesn't exist in group by.""" + query_result = it_helpers.mf_engine.query( + MetricFlowQueryRequest.create_with_random_request_id( + metric_names=("visit_buy_conversions",), + time_constraint_start=datetime.datetime(2020, 1, 1), + time_constraint_end=datetime.datetime(2020, 1, 1), + ) + ) + assert query_result.result_df is not None, "Unexpected empty result." + + assert_str_snapshot_equal( + request=request, + mf_test_configuration=mf_test_configuration, + snapshot_id="query_output", + snapshot_str=query_result.result_df.text_format(), + sql_engine=sql_client.sql_engine_type, + ) diff --git a/tests_metricflow/query_rendering/test_conversion_metric_rendering.py b/tests_metricflow/query_rendering/test_conversion_metric_rendering.py index 6dec9859d5..9f7e4c74fa 100644 --- a/tests_metricflow/query_rendering/test_conversion_metric_rendering.py +++ b/tests_metricflow/query_rendering/test_conversion_metric_rendering.py @@ -164,3 +164,59 @@ def test_conversion_metric_with_window_and_time_constraint( dataflow_plan_builder=dataflow_plan_builder, query_spec=parsed_query.query_spec, ) + + +@pytest.mark.sql_engine_snapshot +def test_conversion_metric_with_filter( + request: FixtureRequest, + mf_test_configuration: MetricFlowTestConfiguration, + dataflow_plan_builder: DataflowPlanBuilder, + dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter, + sql_client: SqlClient, + query_parser: MetricFlowQueryParser, + create_source_tables: bool, +) -> None: + """Test rendering a query against a conversion metric.""" + parsed_query = query_parser.parse_and_validate_query( + metric_names=("visit_buy_conversion_rate",), + where_constraints=( + PydanticWhereFilter(where_sql_template=("{{ TimeDimension('metric_time', 'day') }} = '2020-01-01'")), + ), + ) + + render_and_check( + request=request, + mf_test_configuration=mf_test_configuration, + dataflow_to_sql_converter=dataflow_to_sql_converter, + sql_client=sql_client, + dataflow_plan_builder=dataflow_plan_builder, + query_spec=parsed_query.query_spec, + ) + + +@pytest.mark.sql_engine_snapshot +def test_conversion_metric_with_filter_not_in_group_by( + request: FixtureRequest, + mf_test_configuration: MetricFlowTestConfiguration, + dataflow_plan_builder: DataflowPlanBuilder, + dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter, + sql_client: SqlClient, + query_parser: MetricFlowQueryParser, + create_source_tables: bool, +) -> None: + """Test rendering a query against a conversion metric.""" + parsed_query = query_parser.parse_and_validate_query( + metric_names=("visit_buy_conversions",), + where_constraints=( + PydanticWhereFilter(where_sql_template=("{{ Dimension('visit__referrer_id') }} = 'ref_id_01'")), + ), + ) + + render_and_check( + request=request, + mf_test_configuration=mf_test_configuration, + dataflow_to_sql_converter=dataflow_to_sql_converter, + sql_client=sql_client, + dataflow_plan_builder=dataflow_plan_builder, + query_spec=parsed_query.query_spec, + ) diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric__plan0.sql index fb505c5256..e78459f108 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric__plan0.sql @@ -1,13 +1,13 @@ -- Compute Metrics via Expressions SELECT - subq_15.metric_time__day - , CAST(subq_15.buys AS FLOAT64) / CAST(NULLIF(subq_15.visits, 0) AS FLOAT64) AS visit_buy_conversion_rate + subq_16.metric_time__day + , CAST(subq_16.buys AS FLOAT64) / CAST(NULLIF(subq_16.visits, 0) AS FLOAT64) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) AS metric_time__day + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) AS metric_time__day , MAX(subq_4.visits) AS visits - , MAX(subq_14.buys) AS buys + , MAX(subq_15.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -112,231 +112,277 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_13.metric_time__day - , SUM(subq_13.buys) AS buys + subq_14.metric_time__day + , SUM(subq_14.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'metric_time__day'] SELECT - subq_12.metric_time__day - , subq_12.buys + subq_13.metric_time__day + , subq_13.buys FROM ( -- Find conversions for user within the range of INF SELECT - subq_11.ds__day - , subq_11.metric_time__day - , subq_11.user - , subq_11.buys - , subq_11.visits + subq_12.ds__day + , subq_12.metric_time__day + , subq_12.user + , subq_12.buys + , subq_12.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_7.visits) OVER ( + FIRST_VALUE(subq_8.visits) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_7.ds__day) OVER ( + , FIRST_VALUE(subq_8.ds__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_7.metric_time__day) OVER ( + , FIRST_VALUE(subq_8.metric_time__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_7.user) OVER ( + , FIRST_VALUE(subq_8.user) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_10.mf_internal_uuid AS mf_internal_uuid - , subq_10.buys AS buys + , subq_11.mf_internal_uuid AS mf_internal_uuid + , subq_11.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_6.ds__day - , subq_6.metric_time__day - , subq_6.user - , subq_6.visits + subq_7.ds__day + , subq_7.metric_time__day + , subq_7.user + , subq_7.visits FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE SELECT - subq_5.ds__day - , subq_5.ds__week - , subq_5.ds__month - , subq_5.ds__quarter - , subq_5.ds__year - , subq_5.ds__extract_year - , subq_5.ds__extract_quarter - , subq_5.ds__extract_month - , subq_5.ds__extract_day - , subq_5.ds__extract_dow - , subq_5.ds__extract_doy - , subq_5.visit__ds__day - , subq_5.visit__ds__week - , subq_5.visit__ds__month - , subq_5.visit__ds__quarter - , subq_5.visit__ds__year - , subq_5.visit__ds__extract_year - , subq_5.visit__ds__extract_quarter - , subq_5.visit__ds__extract_month - , subq_5.visit__ds__extract_day - , subq_5.visit__ds__extract_dow - , subq_5.visit__ds__extract_doy - , subq_5.ds__day AS metric_time__day - , subq_5.ds__week AS metric_time__week - , subq_5.ds__month AS metric_time__month - , subq_5.ds__quarter AS metric_time__quarter - , subq_5.ds__year AS metric_time__year - , subq_5.ds__extract_year AS metric_time__extract_year - , subq_5.ds__extract_quarter AS metric_time__extract_quarter - , subq_5.ds__extract_month AS metric_time__extract_month - , subq_5.ds__extract_day AS metric_time__extract_day - , subq_5.ds__extract_dow AS metric_time__extract_dow - , subq_5.ds__extract_doy AS metric_time__extract_doy - , subq_5.user - , subq_5.session - , subq_5.visit__user - , subq_5.visit__session - , subq_5.referrer_id - , subq_5.visit__referrer_id - , subq_5.visits - , subq_5.visitors + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.metric_time__day + , subq_6.metric_time__week + , subq_6.metric_time__month + , subq_6.metric_time__quarter + , subq_6.metric_time__year + , subq_6.metric_time__extract_year + , subq_6.metric_time__extract_quarter + , subq_6.metric_time__extract_month + , subq_6.metric_time__extract_day + , subq_6.metric_time__extract_dow + , subq_6.metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS ds__day - , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS ds__week - , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS ds__month - , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS ds__quarter - , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS ds__extract_dow - , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS visit__ds__day - , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS visit__ds__week - , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS visit__ds__month - , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS visit__ds__quarter - , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS visit__ds__extract_dow - , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_5 - ) subq_6 - ) subq_7 + subq_5.ds__day + , subq_5.ds__week + , subq_5.ds__month + , subq_5.ds__quarter + , subq_5.ds__year + , subq_5.ds__extract_year + , subq_5.ds__extract_quarter + , subq_5.ds__extract_month + , subq_5.ds__extract_day + , subq_5.ds__extract_dow + , subq_5.ds__extract_doy + , subq_5.visit__ds__day + , subq_5.visit__ds__week + , subq_5.visit__ds__month + , subq_5.visit__ds__quarter + , subq_5.visit__ds__year + , subq_5.visit__ds__extract_year + , subq_5.visit__ds__extract_quarter + , subq_5.visit__ds__extract_month + , subq_5.visit__ds__extract_day + , subq_5.visit__ds__extract_dow + , subq_5.visit__ds__extract_doy + , subq_5.ds__day AS metric_time__day + , subq_5.ds__week AS metric_time__week + , subq_5.ds__month AS metric_time__month + , subq_5.ds__quarter AS metric_time__quarter + , subq_5.ds__year AS metric_time__year + , subq_5.ds__extract_year AS metric_time__extract_year + , subq_5.ds__extract_quarter AS metric_time__extract_quarter + , subq_5.ds__extract_month AS metric_time__extract_month + , subq_5.ds__extract_day AS metric_time__extract_day + , subq_5.ds__extract_dow AS metric_time__extract_dow + , subq_5.ds__extract_doy AS metric_time__extract_doy + , subq_5.user + , subq_5.session + , subq_5.visit__user + , subq_5.visit__session + , subq_5.referrer_id + , subq_5.visit__referrer_id + , subq_5.visits + , subq_5.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS ds__day + , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS ds__week + , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS ds__month + , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS ds__quarter + , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS visit__ds__day + , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS visit__ds__week + , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS visit__ds__month + , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS visit__ds__quarter + , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS visit__ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_5 + ) subq_6 + WHERE metric_time__day = '2020-01-01' + ) subq_7 + ) subq_8 INNER JOIN ( -- Add column with generated UUID SELECT - subq_9.ds__day - , subq_9.ds__week - , subq_9.ds__month - , subq_9.ds__quarter - , subq_9.ds__year - , subq_9.ds__extract_year - , subq_9.ds__extract_quarter - , subq_9.ds__extract_month - , subq_9.ds__extract_day - , subq_9.ds__extract_dow - , subq_9.ds__extract_doy - , subq_9.buy__ds__day - , subq_9.buy__ds__week - , subq_9.buy__ds__month - , subq_9.buy__ds__quarter - , subq_9.buy__ds__year - , subq_9.buy__ds__extract_year - , subq_9.buy__ds__extract_quarter - , subq_9.buy__ds__extract_month - , subq_9.buy__ds__extract_day - , subq_9.buy__ds__extract_dow - , subq_9.buy__ds__extract_doy - , subq_9.metric_time__day - , subq_9.metric_time__week - , subq_9.metric_time__month - , subq_9.metric_time__quarter - , subq_9.metric_time__year - , subq_9.metric_time__extract_year - , subq_9.metric_time__extract_quarter - , subq_9.metric_time__extract_month - , subq_9.metric_time__extract_day - , subq_9.metric_time__extract_dow - , subq_9.metric_time__extract_doy - , subq_9.user - , subq_9.session_id - , subq_9.buy__user - , subq_9.buy__session_id - , subq_9.buys - , subq_9.buyers + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.metric_time__day + , subq_10.metric_time__week + , subq_10.metric_time__month + , subq_10.metric_time__quarter + , subq_10.metric_time__year + , subq_10.metric_time__extract_year + , subq_10.metric_time__extract_quarter + , subq_10.metric_time__extract_month + , subq_10.metric_time__extract_day + , subq_10.metric_time__extract_dow + , subq_10.metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers , GENERATE_UUID() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_8.ds__day - , subq_8.ds__week - , subq_8.ds__month - , subq_8.ds__quarter - , subq_8.ds__year - , subq_8.ds__extract_year - , subq_8.ds__extract_quarter - , subq_8.ds__extract_month - , subq_8.ds__extract_day - , subq_8.ds__extract_dow - , subq_8.ds__extract_doy - , subq_8.buy__ds__day - , subq_8.buy__ds__week - , subq_8.buy__ds__month - , subq_8.buy__ds__quarter - , subq_8.buy__ds__year - , subq_8.buy__ds__extract_year - , subq_8.buy__ds__extract_quarter - , subq_8.buy__ds__extract_month - , subq_8.buy__ds__extract_day - , subq_8.buy__ds__extract_dow - , subq_8.buy__ds__extract_doy - , subq_8.ds__day AS metric_time__day - , subq_8.ds__week AS metric_time__week - , subq_8.ds__month AS metric_time__month - , subq_8.ds__quarter AS metric_time__quarter - , subq_8.ds__year AS metric_time__year - , subq_8.ds__extract_year AS metric_time__extract_year - , subq_8.ds__extract_quarter AS metric_time__extract_quarter - , subq_8.ds__extract_month AS metric_time__extract_month - , subq_8.ds__extract_day AS metric_time__extract_day - , subq_8.ds__extract_dow AS metric_time__extract_dow - , subq_8.ds__extract_doy AS metric_time__extract_doy - , subq_8.user - , subq_8.session_id - , subq_8.buy__user - , subq_8.buy__session_id - , subq_8.buys - , subq_8.buyers + subq_9.ds__day + , subq_9.ds__week + , subq_9.ds__month + , subq_9.ds__quarter + , subq_9.ds__year + , subq_9.ds__extract_year + , subq_9.ds__extract_quarter + , subq_9.ds__extract_month + , subq_9.ds__extract_day + , subq_9.ds__extract_dow + , subq_9.ds__extract_doy + , subq_9.buy__ds__day + , subq_9.buy__ds__week + , subq_9.buy__ds__month + , subq_9.buy__ds__quarter + , subq_9.buy__ds__year + , subq_9.buy__ds__extract_year + , subq_9.buy__ds__extract_quarter + , subq_9.buy__ds__extract_month + , subq_9.buy__ds__extract_day + , subq_9.buy__ds__extract_dow + , subq_9.buy__ds__extract_doy + , subq_9.ds__day AS metric_time__day + , subq_9.ds__week AS metric_time__week + , subq_9.ds__month AS metric_time__month + , subq_9.ds__quarter AS metric_time__quarter + , subq_9.ds__year AS metric_time__year + , subq_9.ds__extract_year AS metric_time__extract_year + , subq_9.ds__extract_quarter AS metric_time__extract_quarter + , subq_9.ds__extract_month AS metric_time__extract_month + , subq_9.ds__extract_day AS metric_time__extract_day + , subq_9.ds__extract_dow AS metric_time__extract_dow + , subq_9.ds__extract_doy AS metric_time__extract_doy + , subq_9.user + , subq_9.session_id + , subq_9.buy__user + , subq_9.buy__session_id + , subq_9.buys + , subq_9.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -369,23 +415,23 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_8 - ) subq_9 - ) subq_10 + ) subq_9 + ) subq_10 + ) subq_11 ON ( - subq_7.user = subq_10.user + subq_8.user = subq_11.user ) AND ( - (subq_7.ds__day <= subq_10.ds__day) + (subq_8.ds__day <= subq_11.ds__day) ) - ) subq_11 - ) subq_12 - ) subq_13 + ) subq_12 + ) subq_13 + ) subq_14 GROUP BY metric_time__day - ) subq_14 + ) subq_15 ON - subq_4.metric_time__day = subq_14.metric_time__day + subq_4.metric_time__day = subq_15.metric_time__day GROUP BY metric_time__day -) subq_15 +) subq_16 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric__plan0_optimized.sql index 14790809c8..dc5bb38f49 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric__plan0_optimized.sql @@ -5,9 +5,9 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_20.visits) AS visits - , MAX(subq_30.buys) AS buys + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , MAX(subq_21.visits) AS visits + , MAX(subq_32.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -22,11 +22,11 @@ FROM ( DATETIME_TRUNC(ds, day) AS metric_time__day , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_18 + ) subq_19 WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__day - ) subq_20 + ) subq_21 FULL OUTER JOIN ( -- Find conversions for user within the range of INF -- Pass Only Elements: ['buys', 'metric_time__day'] @@ -37,51 +37,60 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_23.visits) OVER ( + FIRST_VALUE(subq_25.visits) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_23.ds__day) OVER ( + , FIRST_VALUE(subq_25.ds__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_23.metric_time__day) OVER ( + , FIRST_VALUE(subq_25.metric_time__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_23.user) OVER ( + , FIRST_VALUE(subq_25.user) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_26.mf_internal_uuid AS mf_internal_uuid - , subq_26.buys AS buys + , subq_28.mf_internal_uuid AS mf_internal_uuid + , subq_28.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - DATETIME_TRUNC(ds, day) AS ds__day - , DATETIME_TRUNC(ds, day) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + ds__day + , metric_time__day + , subq_23.user + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS ds__day + , DATETIME_TRUNC(ds, day) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_23 + WHERE metric_time__day = '2020-01-01' + ) subq_25 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -92,19 +101,19 @@ FROM ( , 1 AS buys , GENERATE_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_26 + ) subq_28 ON ( - subq_23.user = subq_26.user + subq_25.user = subq_28.user ) AND ( - (subq_23.ds__day <= subq_26.ds__day) + (subq_25.ds__day <= subq_28.ds__day) ) - ) subq_27 + ) subq_29 GROUP BY metric_time__day - ) subq_30 + ) subq_32 ON - subq_20.metric_time__day = subq_30.metric_time__day + subq_21.metric_time__day = subq_32.metric_time__day GROUP BY metric_time__day -) subq_31 +) subq_33 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_categorical_filter__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_categorical_filter__plan0.sql index 55ec032ba4..8271de7612 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_categorical_filter__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_categorical_filter__plan0.sql @@ -1,15 +1,15 @@ -- Compute Metrics via Expressions SELECT - subq_15.metric_time__day - , subq_15.visit__referrer_id - , CAST(subq_15.buys AS FLOAT64) / CAST(NULLIF(subq_15.visits, 0) AS FLOAT64) AS visit_buy_conversion_rate + subq_16.metric_time__day + , subq_16.visit__referrer_id + , CAST(subq_16.buys AS FLOAT64) / CAST(NULLIF(subq_16.visits, 0) AS FLOAT64) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) AS metric_time__day - , COALESCE(subq_4.visit__referrer_id, subq_14.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) AS metric_time__day + , COALESCE(subq_4.visit__referrer_id, subq_15.visit__referrer_id) AS visit__referrer_id , MAX(subq_4.visits) AS visits - , MAX(subq_14.buys) AS buys + , MAX(subq_15.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -118,243 +118,289 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_13.metric_time__day - , subq_13.visit__referrer_id - , SUM(subq_13.buys) AS buys + subq_14.metric_time__day + , subq_14.visit__referrer_id + , SUM(subq_14.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] SELECT - subq_12.metric_time__day - , subq_12.visit__referrer_id - , subq_12.buys + subq_13.metric_time__day + , subq_13.visit__referrer_id + , subq_13.buys FROM ( -- Find conversions for user within the range of INF SELECT - subq_11.ds__day - , subq_11.metric_time__day - , subq_11.user - , subq_11.visit__referrer_id - , subq_11.buys - , subq_11.visits + subq_12.ds__day + , subq_12.metric_time__day + , subq_12.user + , subq_12.visit__referrer_id + , subq_12.buys + , subq_12.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_7.visits) OVER ( + FIRST_VALUE(subq_8.visits) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_7.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_8.visit__referrer_id) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_7.ds__day) OVER ( + , FIRST_VALUE(subq_8.ds__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_7.metric_time__day) OVER ( + , FIRST_VALUE(subq_8.metric_time__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_7.user) OVER ( + , FIRST_VALUE(subq_8.user) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_10.mf_internal_uuid AS mf_internal_uuid - , subq_10.buys AS buys + , subq_11.mf_internal_uuid AS mf_internal_uuid + , subq_11.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_6.ds__day - , subq_6.metric_time__day - , subq_6.user - , subq_6.visit__referrer_id - , subq_6.visits + subq_7.ds__day + , subq_7.metric_time__day + , subq_7.user + , subq_7.visit__referrer_id + , subq_7.visits FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE SELECT - subq_5.ds__day - , subq_5.ds__week - , subq_5.ds__month - , subq_5.ds__quarter - , subq_5.ds__year - , subq_5.ds__extract_year - , subq_5.ds__extract_quarter - , subq_5.ds__extract_month - , subq_5.ds__extract_day - , subq_5.ds__extract_dow - , subq_5.ds__extract_doy - , subq_5.visit__ds__day - , subq_5.visit__ds__week - , subq_5.visit__ds__month - , subq_5.visit__ds__quarter - , subq_5.visit__ds__year - , subq_5.visit__ds__extract_year - , subq_5.visit__ds__extract_quarter - , subq_5.visit__ds__extract_month - , subq_5.visit__ds__extract_day - , subq_5.visit__ds__extract_dow - , subq_5.visit__ds__extract_doy - , subq_5.ds__day AS metric_time__day - , subq_5.ds__week AS metric_time__week - , subq_5.ds__month AS metric_time__month - , subq_5.ds__quarter AS metric_time__quarter - , subq_5.ds__year AS metric_time__year - , subq_5.ds__extract_year AS metric_time__extract_year - , subq_5.ds__extract_quarter AS metric_time__extract_quarter - , subq_5.ds__extract_month AS metric_time__extract_month - , subq_5.ds__extract_day AS metric_time__extract_day - , subq_5.ds__extract_dow AS metric_time__extract_dow - , subq_5.ds__extract_doy AS metric_time__extract_doy - , subq_5.user - , subq_5.session - , subq_5.visit__user - , subq_5.visit__session - , subq_5.referrer_id - , subq_5.visit__referrer_id - , subq_5.visits - , subq_5.visitors + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.metric_time__day + , subq_6.metric_time__week + , subq_6.metric_time__month + , subq_6.metric_time__quarter + , subq_6.metric_time__year + , subq_6.metric_time__extract_year + , subq_6.metric_time__extract_quarter + , subq_6.metric_time__extract_month + , subq_6.metric_time__extract_day + , subq_6.metric_time__extract_dow + , subq_6.metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS ds__day - , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS ds__week - , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS ds__month - , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS ds__quarter - , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS ds__extract_dow - , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS visit__ds__day - , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS visit__ds__week - , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS visit__ds__month - , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS visit__ds__quarter - , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS visit__ds__extract_dow - , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_5 - ) subq_6 - ) subq_7 + subq_5.ds__day + , subq_5.ds__week + , subq_5.ds__month + , subq_5.ds__quarter + , subq_5.ds__year + , subq_5.ds__extract_year + , subq_5.ds__extract_quarter + , subq_5.ds__extract_month + , subq_5.ds__extract_day + , subq_5.ds__extract_dow + , subq_5.ds__extract_doy + , subq_5.visit__ds__day + , subq_5.visit__ds__week + , subq_5.visit__ds__month + , subq_5.visit__ds__quarter + , subq_5.visit__ds__year + , subq_5.visit__ds__extract_year + , subq_5.visit__ds__extract_quarter + , subq_5.visit__ds__extract_month + , subq_5.visit__ds__extract_day + , subq_5.visit__ds__extract_dow + , subq_5.visit__ds__extract_doy + , subq_5.ds__day AS metric_time__day + , subq_5.ds__week AS metric_time__week + , subq_5.ds__month AS metric_time__month + , subq_5.ds__quarter AS metric_time__quarter + , subq_5.ds__year AS metric_time__year + , subq_5.ds__extract_year AS metric_time__extract_year + , subq_5.ds__extract_quarter AS metric_time__extract_quarter + , subq_5.ds__extract_month AS metric_time__extract_month + , subq_5.ds__extract_day AS metric_time__extract_day + , subq_5.ds__extract_dow AS metric_time__extract_dow + , subq_5.ds__extract_doy AS metric_time__extract_doy + , subq_5.user + , subq_5.session + , subq_5.visit__user + , subq_5.visit__session + , subq_5.referrer_id + , subq_5.visit__referrer_id + , subq_5.visits + , subq_5.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS ds__day + , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS ds__week + , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS ds__month + , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS ds__quarter + , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS visit__ds__day + , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS visit__ds__week + , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS visit__ds__month + , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS visit__ds__quarter + , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS visit__ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_5 + ) subq_6 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_7 + ) subq_8 INNER JOIN ( -- Add column with generated UUID SELECT - subq_9.ds__day - , subq_9.ds__week - , subq_9.ds__month - , subq_9.ds__quarter - , subq_9.ds__year - , subq_9.ds__extract_year - , subq_9.ds__extract_quarter - , subq_9.ds__extract_month - , subq_9.ds__extract_day - , subq_9.ds__extract_dow - , subq_9.ds__extract_doy - , subq_9.buy__ds__day - , subq_9.buy__ds__week - , subq_9.buy__ds__month - , subq_9.buy__ds__quarter - , subq_9.buy__ds__year - , subq_9.buy__ds__extract_year - , subq_9.buy__ds__extract_quarter - , subq_9.buy__ds__extract_month - , subq_9.buy__ds__extract_day - , subq_9.buy__ds__extract_dow - , subq_9.buy__ds__extract_doy - , subq_9.metric_time__day - , subq_9.metric_time__week - , subq_9.metric_time__month - , subq_9.metric_time__quarter - , subq_9.metric_time__year - , subq_9.metric_time__extract_year - , subq_9.metric_time__extract_quarter - , subq_9.metric_time__extract_month - , subq_9.metric_time__extract_day - , subq_9.metric_time__extract_dow - , subq_9.metric_time__extract_doy - , subq_9.user - , subq_9.session_id - , subq_9.buy__user - , subq_9.buy__session_id - , subq_9.buys - , subq_9.buyers + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.metric_time__day + , subq_10.metric_time__week + , subq_10.metric_time__month + , subq_10.metric_time__quarter + , subq_10.metric_time__year + , subq_10.metric_time__extract_year + , subq_10.metric_time__extract_quarter + , subq_10.metric_time__extract_month + , subq_10.metric_time__extract_day + , subq_10.metric_time__extract_dow + , subq_10.metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers , GENERATE_UUID() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_8.ds__day - , subq_8.ds__week - , subq_8.ds__month - , subq_8.ds__quarter - , subq_8.ds__year - , subq_8.ds__extract_year - , subq_8.ds__extract_quarter - , subq_8.ds__extract_month - , subq_8.ds__extract_day - , subq_8.ds__extract_dow - , subq_8.ds__extract_doy - , subq_8.buy__ds__day - , subq_8.buy__ds__week - , subq_8.buy__ds__month - , subq_8.buy__ds__quarter - , subq_8.buy__ds__year - , subq_8.buy__ds__extract_year - , subq_8.buy__ds__extract_quarter - , subq_8.buy__ds__extract_month - , subq_8.buy__ds__extract_day - , subq_8.buy__ds__extract_dow - , subq_8.buy__ds__extract_doy - , subq_8.ds__day AS metric_time__day - , subq_8.ds__week AS metric_time__week - , subq_8.ds__month AS metric_time__month - , subq_8.ds__quarter AS metric_time__quarter - , subq_8.ds__year AS metric_time__year - , subq_8.ds__extract_year AS metric_time__extract_year - , subq_8.ds__extract_quarter AS metric_time__extract_quarter - , subq_8.ds__extract_month AS metric_time__extract_month - , subq_8.ds__extract_day AS metric_time__extract_day - , subq_8.ds__extract_dow AS metric_time__extract_dow - , subq_8.ds__extract_doy AS metric_time__extract_doy - , subq_8.user - , subq_8.session_id - , subq_8.buy__user - , subq_8.buy__session_id - , subq_8.buys - , subq_8.buyers + subq_9.ds__day + , subq_9.ds__week + , subq_9.ds__month + , subq_9.ds__quarter + , subq_9.ds__year + , subq_9.ds__extract_year + , subq_9.ds__extract_quarter + , subq_9.ds__extract_month + , subq_9.ds__extract_day + , subq_9.ds__extract_dow + , subq_9.ds__extract_doy + , subq_9.buy__ds__day + , subq_9.buy__ds__week + , subq_9.buy__ds__month + , subq_9.buy__ds__quarter + , subq_9.buy__ds__year + , subq_9.buy__ds__extract_year + , subq_9.buy__ds__extract_quarter + , subq_9.buy__ds__extract_month + , subq_9.buy__ds__extract_day + , subq_9.buy__ds__extract_dow + , subq_9.buy__ds__extract_doy + , subq_9.ds__day AS metric_time__day + , subq_9.ds__week AS metric_time__week + , subq_9.ds__month AS metric_time__month + , subq_9.ds__quarter AS metric_time__quarter + , subq_9.ds__year AS metric_time__year + , subq_9.ds__extract_year AS metric_time__extract_year + , subq_9.ds__extract_quarter AS metric_time__extract_quarter + , subq_9.ds__extract_month AS metric_time__extract_month + , subq_9.ds__extract_day AS metric_time__extract_day + , subq_9.ds__extract_dow AS metric_time__extract_dow + , subq_9.ds__extract_doy AS metric_time__extract_doy + , subq_9.user + , subq_9.session_id + , subq_9.buy__user + , subq_9.buy__session_id + , subq_9.buys + , subq_9.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -387,29 +433,29 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_8 - ) subq_9 - ) subq_10 + ) subq_9 + ) subq_10 + ) subq_11 ON ( - subq_7.user = subq_10.user + subq_8.user = subq_11.user ) AND ( - (subq_7.ds__day <= subq_10.ds__day) + (subq_8.ds__day <= subq_11.ds__day) ) - ) subq_11 - ) subq_12 - ) subq_13 + ) subq_12 + ) subq_13 + ) subq_14 GROUP BY metric_time__day , visit__referrer_id - ) subq_14 + ) subq_15 ON ( - subq_4.visit__referrer_id = subq_14.visit__referrer_id + subq_4.visit__referrer_id = subq_15.visit__referrer_id ) AND ( - subq_4.metric_time__day = subq_14.metric_time__day + subq_4.metric_time__day = subq_15.metric_time__day ) GROUP BY metric_time__day , visit__referrer_id -) subq_15 +) subq_16 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_categorical_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_categorical_filter__plan0_optimized.sql index acfdeb4045..74607a032f 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_categorical_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_categorical_filter__plan0_optimized.sql @@ -6,10 +6,10 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , COALESCE(subq_20.visit__referrer_id, subq_30.visit__referrer_id) AS visit__referrer_id - , MAX(subq_20.visits) AS visits - , MAX(subq_30.buys) AS buys + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , COALESCE(subq_21.visit__referrer_id, subq_32.visit__referrer_id) AS visit__referrer_id + , MAX(subq_21.visits) AS visits + , MAX(subq_32.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -26,12 +26,12 @@ FROM ( , referrer_id AS visit__referrer_id , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_18 + ) subq_19 WHERE visit__referrer_id = 'ref_id_01' GROUP BY metric_time__day , visit__referrer_id - ) subq_20 + ) subq_21 FULL OUTER JOIN ( -- Find conversions for user within the range of INF -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] @@ -43,60 +43,70 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_23.visits) OVER ( + FIRST_VALUE(subq_25.visits) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_23.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_25.visit__referrer_id) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_23.ds__day) OVER ( + , FIRST_VALUE(subq_25.ds__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_23.metric_time__day) OVER ( + , FIRST_VALUE(subq_25.metric_time__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_23.user) OVER ( + , FIRST_VALUE(subq_25.user) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_26.mf_internal_uuid AS mf_internal_uuid - , subq_26.buys AS buys + , subq_28.mf_internal_uuid AS mf_internal_uuid + , subq_28.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - DATETIME_TRUNC(ds, day) AS ds__day - , 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_23 + ds__day + , metric_time__day + , subq_23.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS ds__day + , 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_23 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_25 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -107,25 +117,25 @@ FROM ( , 1 AS buys , GENERATE_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_26 + ) subq_28 ON ( - subq_23.user = subq_26.user + subq_25.user = subq_28.user ) AND ( - (subq_23.ds__day <= subq_26.ds__day) + (subq_25.ds__day <= subq_28.ds__day) ) - ) subq_27 + ) subq_29 GROUP BY metric_time__day , visit__referrer_id - ) subq_30 + ) subq_32 ON ( - subq_20.visit__referrer_id = subq_30.visit__referrer_id + subq_21.visit__referrer_id = subq_32.visit__referrer_id ) AND ( - subq_20.metric_time__day = subq_30.metric_time__day + subq_21.metric_time__day = subq_32.metric_time__day ) GROUP BY metric_time__day , visit__referrer_id -) subq_31 +) subq_33 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_filter__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_filter__plan0.sql new file mode 100644 index 0000000000..55e55cded2 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_filter__plan0.sql @@ -0,0 +1,429 @@ +-- Compute Metrics via Expressions +SELECT + CAST(subq_17.buys AS FLOAT64) / CAST(NULLIF(subq_17.visits, 0) AS FLOAT64) AS visit_buy_conversion_rate +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_5.visits) AS visits + , MAX(subq_16.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_4.visits) AS visits + FROM ( + -- Pass Only Elements: ['visits',] + SELECT + subq_3.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_2.metric_time__day + , subq_2.visits + FROM ( + -- Pass Only Elements: ['visits', 'metric_time__day'] + SELECT + subq_1.metric_time__day + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS ds__day + , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS ds__week + , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS ds__month + , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS ds__quarter + , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS visit__ds__day + , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS visit__ds__week + , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS visit__ds__month + , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS visit__ds__quarter + , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS visit__ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_0 + ) subq_1 + ) subq_2 + WHERE metric_time__day = '2020-01-01' + ) subq_3 + ) subq_4 + ) subq_5 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_15.buys) AS buys + FROM ( + -- Pass Only Elements: ['buys',] + SELECT + subq_14.buys + FROM ( + -- Find conversions for user within the range of INF + SELECT + subq_13.ds__day + , subq_13.metric_time__day + , subq_13.user + , subq_13.buys + , subq_13.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_9.visits) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_9.ds__day) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_9.metric_time__day) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS metric_time__day + , FIRST_VALUE(subq_9.user) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_12.mf_internal_uuid AS mf_internal_uuid + , subq_12.buys AS buys + FROM ( + -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] + SELECT + subq_8.ds__day + , subq_8.metric_time__day + , subq_8.user + , subq_8.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS ds__day + , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS ds__week + , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS ds__month + , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS ds__quarter + , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS visit__ds__day + , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS visit__ds__week + , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS visit__ds__month + , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS visit__ds__quarter + , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS visit__ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE metric_time__day = '2020-01-01' + ) subq_8 + ) subq_9 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.metric_time__day + , subq_11.metric_time__week + , subq_11.metric_time__month + , subq_11.metric_time__quarter + , subq_11.metric_time__year + , subq_11.metric_time__extract_year + , subq_11.metric_time__extract_quarter + , subq_11.metric_time__extract_month + , subq_11.metric_time__extract_day + , subq_11.metric_time__extract_dow + , subq_11.metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers + , GENERATE_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.ds__day AS metric_time__day + , subq_10.ds__week AS metric_time__week + , subq_10.ds__month AS metric_time__month + , subq_10.ds__quarter AS metric_time__quarter + , subq_10.ds__year AS metric_time__year + , subq_10.ds__extract_year AS metric_time__extract_year + , subq_10.ds__extract_quarter AS metric_time__extract_quarter + , subq_10.ds__extract_month AS metric_time__extract_month + , subq_10.ds__extract_day AS metric_time__extract_day + , subq_10.ds__extract_dow AS metric_time__extract_dow + , subq_10.ds__extract_doy AS metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_28000.user_id AS buyers + , DATETIME_TRUNC(buys_source_src_28000.ds, day) AS ds__day + , DATETIME_TRUNC(buys_source_src_28000.ds, isoweek) AS ds__week + , DATETIME_TRUNC(buys_source_src_28000.ds, month) AS ds__month + , DATETIME_TRUNC(buys_source_src_28000.ds, quarter) AS ds__quarter + , DATETIME_TRUNC(buys_source_src_28000.ds, year) AS ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM buys_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM buys_source_src_28000.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM buys_source_src_28000.ds) AS ds__extract_doy + , DATETIME_TRUNC(buys_source_src_28000.ds, day) AS buy__ds__day + , DATETIME_TRUNC(buys_source_src_28000.ds, isoweek) AS buy__ds__week + , DATETIME_TRUNC(buys_source_src_28000.ds, month) AS buy__ds__month + , DATETIME_TRUNC(buys_source_src_28000.ds, quarter) AS buy__ds__quarter + , DATETIME_TRUNC(buys_source_src_28000.ds, year) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day + , IF(EXTRACT(dayofweek FROM buys_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM buys_source_src_28000.ds) - 1) AS buy__ds__extract_dow + , EXTRACT(dayofyear FROM buys_source_src_28000.ds) AS buy__ds__extract_doy + , buys_source_src_28000.user_id AS user + , buys_source_src_28000.session_id + , buys_source_src_28000.user_id AS buy__user + , buys_source_src_28000.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_10 + ) subq_11 + ) subq_12 + ON + ( + subq_9.user = subq_12.user + ) AND ( + (subq_9.ds__day <= subq_12.ds__day) + ) + ) subq_13 + ) subq_14 + ) subq_15 + ) subq_16 +) subq_17 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_filter__plan0_optimized.sql new file mode 100644 index 0000000000..4441c2a66c --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_filter__plan0_optimized.sql @@ -0,0 +1,103 @@ +-- Combine Aggregated Outputs +-- Compute Metrics via Expressions +SELECT + CAST(MAX(subq_34.buys) AS FLOAT64) / CAST(NULLIF(MAX(subq_23.visits), 0) AS FLOAT64) AS visit_buy_conversion_rate +FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits',] + -- Aggregate Measures + SELECT + 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_20 + WHERE metric_time__day = '2020-01-01' +) subq_23 +CROSS JOIN ( + -- Find conversions for user within the range of INF + -- Pass Only Elements: ['buys',] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_27.visits) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_27.ds__day) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_27.metric_time__day) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS metric_time__day + , FIRST_VALUE(subq_27.user) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_30.mf_internal_uuid AS mf_internal_uuid + , subq_30.buys AS buys + FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] + SELECT + ds__day + , metric_time__day + , subq_25.user + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS ds__day + , DATETIME_TRUNC(ds, day) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_25 + WHERE metric_time__day = '2020-01-01' + ) subq_27 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATETIME_TRUNC(ds, day) AS ds__day + , user_id AS user + , 1 AS buys + , GENERATE_UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_30 + ON + ( + subq_27.user = subq_30.user + ) AND ( + (subq_27.ds__day <= subq_30.ds__day) + ) + ) subq_31 +) subq_34 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_filter_not_in_group_by__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_filter_not_in_group_by__plan0.sql new file mode 100644 index 0000000000..c3185029bd --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_filter_not_in_group_by__plan0.sql @@ -0,0 +1,433 @@ +-- Compute Metrics via Expressions +SELECT + subq_17.buys AS visit_buy_conversions +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_5.visits) AS visits + , COALESCE(MAX(subq_16.buys), 0) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_4.visits) AS visits + FROM ( + -- Pass Only Elements: ['visits',] + SELECT + subq_3.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_2.visit__referrer_id + , subq_2.visits + FROM ( + -- Pass Only Elements: ['visits', 'visit__referrer_id'] + SELECT + subq_1.visit__referrer_id + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS ds__day + , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS ds__week + , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS ds__month + , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS ds__quarter + , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS visit__ds__day + , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS visit__ds__week + , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS visit__ds__month + , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS visit__ds__quarter + , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS visit__ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_0 + ) subq_1 + ) subq_2 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_3 + ) subq_4 + ) subq_5 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_15.buys) AS buys + FROM ( + -- Pass Only Elements: ['buys',] + SELECT + subq_14.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_13.ds__day + , subq_13.user + , subq_13.visit__referrer_id + , subq_13.buys + , subq_13.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_9.visits) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_9.visit__referrer_id) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visit__referrer_id + , FIRST_VALUE(subq_9.ds__day) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_9.user) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_12.mf_internal_uuid AS mf_internal_uuid + , subq_12.buys AS buys + FROM ( + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] + SELECT + subq_8.ds__day + , subq_8.user + , subq_8.visit__referrer_id + , subq_8.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS ds__day + , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS ds__week + , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS ds__month + , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS ds__quarter + , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS visit__ds__day + , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS visit__ds__week + , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS visit__ds__month + , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS visit__ds__quarter + , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS visit__ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_8 + ) subq_9 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.metric_time__day + , subq_11.metric_time__week + , subq_11.metric_time__month + , subq_11.metric_time__quarter + , subq_11.metric_time__year + , subq_11.metric_time__extract_year + , subq_11.metric_time__extract_quarter + , subq_11.metric_time__extract_month + , subq_11.metric_time__extract_day + , subq_11.metric_time__extract_dow + , subq_11.metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers + , GENERATE_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.ds__day AS metric_time__day + , subq_10.ds__week AS metric_time__week + , subq_10.ds__month AS metric_time__month + , subq_10.ds__quarter AS metric_time__quarter + , subq_10.ds__year AS metric_time__year + , subq_10.ds__extract_year AS metric_time__extract_year + , subq_10.ds__extract_quarter AS metric_time__extract_quarter + , subq_10.ds__extract_month AS metric_time__extract_month + , subq_10.ds__extract_day AS metric_time__extract_day + , subq_10.ds__extract_dow AS metric_time__extract_dow + , subq_10.ds__extract_doy AS metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_28000.user_id AS buyers + , DATETIME_TRUNC(buys_source_src_28000.ds, day) AS ds__day + , DATETIME_TRUNC(buys_source_src_28000.ds, isoweek) AS ds__week + , DATETIME_TRUNC(buys_source_src_28000.ds, month) AS ds__month + , DATETIME_TRUNC(buys_source_src_28000.ds, quarter) AS ds__quarter + , DATETIME_TRUNC(buys_source_src_28000.ds, year) AS ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM buys_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM buys_source_src_28000.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM buys_source_src_28000.ds) AS ds__extract_doy + , DATETIME_TRUNC(buys_source_src_28000.ds, day) AS buy__ds__day + , DATETIME_TRUNC(buys_source_src_28000.ds, isoweek) AS buy__ds__week + , DATETIME_TRUNC(buys_source_src_28000.ds, month) AS buy__ds__month + , DATETIME_TRUNC(buys_source_src_28000.ds, quarter) AS buy__ds__quarter + , DATETIME_TRUNC(buys_source_src_28000.ds, year) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day + , IF(EXTRACT(dayofweek FROM buys_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM buys_source_src_28000.ds) - 1) AS buy__ds__extract_dow + , EXTRACT(dayofyear FROM buys_source_src_28000.ds) AS buy__ds__extract_doy + , buys_source_src_28000.user_id AS user + , buys_source_src_28000.session_id + , buys_source_src_28000.user_id AS buy__user + , buys_source_src_28000.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_10 + ) subq_11 + ) subq_12 + ON + ( + subq_9.user = subq_12.user + ) AND ( + ( + subq_9.ds__day <= subq_12.ds__day + ) AND ( + subq_9.ds__day > DATE_SUB(CAST(subq_12.ds__day AS DATETIME), INTERVAL 7 day) + ) + ) + ) subq_13 + ) subq_14 + ) subq_15 + ) subq_16 +) subq_17 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_filter_not_in_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_filter_not_in_group_by__plan0_optimized.sql new file mode 100644 index 0000000000..022d91417f --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_filter_not_in_group_by__plan0_optimized.sql @@ -0,0 +1,107 @@ +-- Combine Aggregated Outputs +-- Compute Metrics via Expressions +SELECT + COALESCE(MAX(subq_34.buys), 0) AS visit_buy_conversions +FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits',] + -- Aggregate Measures + SELECT + 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_20 + WHERE visit__referrer_id = 'ref_id_01' +) subq_23 +CROSS JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: ['buys',] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_27.visits) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_27.visit__referrer_id) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visit__referrer_id + , FIRST_VALUE(subq_27.ds__day) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_27.user) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_30.mf_internal_uuid AS mf_internal_uuid + , subq_30.buys AS buys + FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] + SELECT + ds__day + , subq_25.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS ds__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_25 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_27 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATETIME_TRUNC(ds, day) AS ds__day + , user_id AS user + , 1 AS buys + , GENERATE_UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_30 + ON + ( + subq_27.user = subq_30.user + ) AND ( + ( + subq_27.ds__day <= subq_30.ds__day + ) AND ( + subq_27.ds__day > DATE_SUB(CAST(subq_30.ds__day AS DATETIME), INTERVAL 7 day) + ) + ) + ) subq_31 +) subq_34 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_time_constraint__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_time_constraint__plan0.sql index eb3dbe8b63..df1b498c4f 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_time_constraint__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_time_constraint__plan0.sql @@ -1,13 +1,13 @@ -- Compute Metrics via Expressions SELECT - subq_17.visit__referrer_id - , CAST(subq_17.buys AS FLOAT64) / CAST(NULLIF(subq_17.visits, 0) AS FLOAT64) AS visit_buy_conversion_rate + subq_18.visit__referrer_id + , CAST(subq_18.buys AS FLOAT64) / CAST(NULLIF(subq_18.visits, 0) AS FLOAT64) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) AS visit__referrer_id , MAX(subq_5.visits) AS visits - , MAX(subq_16.buys) AS buys + , MAX(subq_17.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -158,277 +158,323 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_15.visit__referrer_id - , SUM(subq_15.buys) AS buys + subq_16.visit__referrer_id + , SUM(subq_16.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'visit__referrer_id'] SELECT - subq_14.visit__referrer_id - , subq_14.buys + subq_15.visit__referrer_id + , subq_15.buys FROM ( -- Find conversions for user within the range of INF SELECT - subq_13.ds__day - , subq_13.user - , subq_13.visit__referrer_id - , subq_13.buys - , subq_13.visits + subq_14.ds__day + , subq_14.user + , subq_14.visit__referrer_id + , subq_14.buys + , subq_14.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_9.visits) OVER ( + FIRST_VALUE(subq_10.visits) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_9.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_10.visit__referrer_id) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_9.ds__day) OVER ( + , FIRST_VALUE(subq_10.ds__day) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_9.user) OVER ( + , FIRST_VALUE(subq_10.user) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_12.mf_internal_uuid AS mf_internal_uuid - , subq_12.buys AS buys + , subq_13.mf_internal_uuid AS mf_internal_uuid + , subq_13.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] SELECT - subq_8.ds__day - , subq_8.user - , subq_8.visit__referrer_id - , subq_8.visits + subq_9.ds__day + , subq_9.user + , subq_9.visit__referrer_id + , subq_9.visits FROM ( - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE SELECT - subq_7.ds__day - , subq_7.ds__week - , subq_7.ds__month - , subq_7.ds__quarter - , subq_7.ds__year - , subq_7.ds__extract_year - , subq_7.ds__extract_quarter - , subq_7.ds__extract_month - , subq_7.ds__extract_day - , subq_7.ds__extract_dow - , subq_7.ds__extract_doy - , subq_7.visit__ds__day - , subq_7.visit__ds__week - , subq_7.visit__ds__month - , subq_7.visit__ds__quarter - , subq_7.visit__ds__year - , subq_7.visit__ds__extract_year - , subq_7.visit__ds__extract_quarter - , subq_7.visit__ds__extract_month - , subq_7.visit__ds__extract_day - , subq_7.visit__ds__extract_dow - , subq_7.visit__ds__extract_doy - , subq_7.metric_time__day - , subq_7.metric_time__week - , subq_7.metric_time__month - , subq_7.metric_time__quarter - , subq_7.metric_time__year - , subq_7.metric_time__extract_year - , subq_7.metric_time__extract_quarter - , subq_7.metric_time__extract_month - , subq_7.metric_time__extract_day - , subq_7.metric_time__extract_dow - , subq_7.metric_time__extract_doy - , subq_7.user - , subq_7.session - , subq_7.visit__user - , subq_7.visit__session - , subq_7.referrer_id - , subq_7.visit__referrer_id - , subq_7.visits - , subq_7.visitors + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.visit__ds__day + , subq_8.visit__ds__week + , subq_8.visit__ds__month + , subq_8.visit__ds__quarter + , subq_8.visit__ds__year + , subq_8.visit__ds__extract_year + , subq_8.visit__ds__extract_quarter + , subq_8.visit__ds__extract_month + , subq_8.visit__ds__extract_day + , subq_8.visit__ds__extract_dow + , subq_8.visit__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session + , subq_8.visit__user + , subq_8.visit__session + , subq_8.referrer_id + , subq_8.visit__referrer_id + , subq_8.visits + , subq_8.visitors FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] SELECT - subq_6.ds__day - , subq_6.ds__week - , subq_6.ds__month - , subq_6.ds__quarter - , subq_6.ds__year - , subq_6.ds__extract_year - , subq_6.ds__extract_quarter - , subq_6.ds__extract_month - , subq_6.ds__extract_day - , subq_6.ds__extract_dow - , subq_6.ds__extract_doy - , subq_6.visit__ds__day - , subq_6.visit__ds__week - , subq_6.visit__ds__month - , subq_6.visit__ds__quarter - , subq_6.visit__ds__year - , subq_6.visit__ds__extract_year - , subq_6.visit__ds__extract_quarter - , subq_6.visit__ds__extract_month - , subq_6.visit__ds__extract_day - , subq_6.visit__ds__extract_dow - , subq_6.visit__ds__extract_doy - , subq_6.ds__day AS metric_time__day - , subq_6.ds__week AS metric_time__week - , subq_6.ds__month AS metric_time__month - , subq_6.ds__quarter AS metric_time__quarter - , subq_6.ds__year AS metric_time__year - , subq_6.ds__extract_year AS metric_time__extract_year - , subq_6.ds__extract_quarter AS metric_time__extract_quarter - , subq_6.ds__extract_month AS metric_time__extract_month - , subq_6.ds__extract_day AS metric_time__extract_day - , subq_6.ds__extract_dow AS metric_time__extract_dow - , subq_6.ds__extract_doy AS metric_time__extract_doy - , subq_6.user - , subq_6.session - , subq_6.visit__user - , subq_6.visit__session - , subq_6.referrer_id - , subq_6.visit__referrer_id - , subq_6.visits - , subq_6.visitors + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS ds__day - , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS ds__week - , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS ds__month - , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS ds__quarter - , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS ds__extract_dow - , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS visit__ds__day - , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS visit__ds__week - , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS visit__ds__month - , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS visit__ds__quarter - , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS visit__ds__extract_dow - , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_6 - ) subq_7 - WHERE subq_7.metric_time__day BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_8 - ) subq_9 + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS ds__day + , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS ds__week + , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS ds__month + , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS ds__quarter + , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS visit__ds__day + , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS visit__ds__week + , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS visit__ds__month + , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS visit__ds__quarter + , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS visit__ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE subq_7.metric_time__day BETWEEN '2020-01-01' AND '2020-01-02' + ) subq_8 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_9 + ) subq_10 INNER JOIN ( -- Add column with generated UUID SELECT - subq_11.ds__day - , subq_11.ds__week - , subq_11.ds__month - , subq_11.ds__quarter - , subq_11.ds__year - , subq_11.ds__extract_year - , subq_11.ds__extract_quarter - , subq_11.ds__extract_month - , subq_11.ds__extract_day - , subq_11.ds__extract_dow - , subq_11.ds__extract_doy - , subq_11.buy__ds__day - , subq_11.buy__ds__week - , subq_11.buy__ds__month - , subq_11.buy__ds__quarter - , subq_11.buy__ds__year - , subq_11.buy__ds__extract_year - , subq_11.buy__ds__extract_quarter - , subq_11.buy__ds__extract_month - , subq_11.buy__ds__extract_day - , subq_11.buy__ds__extract_dow - , subq_11.buy__ds__extract_doy - , subq_11.metric_time__day - , subq_11.metric_time__week - , subq_11.metric_time__month - , subq_11.metric_time__quarter - , subq_11.metric_time__year - , subq_11.metric_time__extract_year - , subq_11.metric_time__extract_quarter - , subq_11.metric_time__extract_month - , subq_11.metric_time__extract_day - , subq_11.metric_time__extract_dow - , subq_11.metric_time__extract_doy - , subq_11.user - , subq_11.session_id - , subq_11.buy__user - , subq_11.buy__session_id - , subq_11.buys - , subq_11.buyers + subq_12.ds__day + , subq_12.ds__week + , subq_12.ds__month + , subq_12.ds__quarter + , subq_12.ds__year + , subq_12.ds__extract_year + , subq_12.ds__extract_quarter + , subq_12.ds__extract_month + , subq_12.ds__extract_day + , subq_12.ds__extract_dow + , subq_12.ds__extract_doy + , subq_12.buy__ds__day + , subq_12.buy__ds__week + , subq_12.buy__ds__month + , subq_12.buy__ds__quarter + , subq_12.buy__ds__year + , subq_12.buy__ds__extract_year + , subq_12.buy__ds__extract_quarter + , subq_12.buy__ds__extract_month + , subq_12.buy__ds__extract_day + , subq_12.buy__ds__extract_dow + , subq_12.buy__ds__extract_doy + , subq_12.metric_time__day + , subq_12.metric_time__week + , subq_12.metric_time__month + , subq_12.metric_time__quarter + , subq_12.metric_time__year + , subq_12.metric_time__extract_year + , subq_12.metric_time__extract_quarter + , subq_12.metric_time__extract_month + , subq_12.metric_time__extract_day + , subq_12.metric_time__extract_dow + , subq_12.metric_time__extract_doy + , subq_12.user + , subq_12.session_id + , subq_12.buy__user + , subq_12.buy__session_id + , subq_12.buys + , subq_12.buyers , GENERATE_UUID() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_10.ds__day - , subq_10.ds__week - , subq_10.ds__month - , subq_10.ds__quarter - , subq_10.ds__year - , subq_10.ds__extract_year - , subq_10.ds__extract_quarter - , subq_10.ds__extract_month - , subq_10.ds__extract_day - , subq_10.ds__extract_dow - , subq_10.ds__extract_doy - , subq_10.buy__ds__day - , subq_10.buy__ds__week - , subq_10.buy__ds__month - , subq_10.buy__ds__quarter - , subq_10.buy__ds__year - , subq_10.buy__ds__extract_year - , subq_10.buy__ds__extract_quarter - , subq_10.buy__ds__extract_month - , subq_10.buy__ds__extract_day - , subq_10.buy__ds__extract_dow - , subq_10.buy__ds__extract_doy - , subq_10.ds__day AS metric_time__day - , subq_10.ds__week AS metric_time__week - , subq_10.ds__month AS metric_time__month - , subq_10.ds__quarter AS metric_time__quarter - , subq_10.ds__year AS metric_time__year - , subq_10.ds__extract_year AS metric_time__extract_year - , subq_10.ds__extract_quarter AS metric_time__extract_quarter - , subq_10.ds__extract_month AS metric_time__extract_month - , subq_10.ds__extract_day AS metric_time__extract_day - , subq_10.ds__extract_dow AS metric_time__extract_dow - , subq_10.ds__extract_doy AS metric_time__extract_doy - , subq_10.user - , subq_10.session_id - , subq_10.buy__user - , subq_10.buy__session_id - , subq_10.buys - , subq_10.buyers + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.ds__day AS metric_time__day + , subq_11.ds__week AS metric_time__week + , subq_11.ds__month AS metric_time__month + , subq_11.ds__quarter AS metric_time__quarter + , subq_11.ds__year AS metric_time__year + , subq_11.ds__extract_year AS metric_time__extract_year + , subq_11.ds__extract_quarter AS metric_time__extract_quarter + , subq_11.ds__extract_month AS metric_time__extract_month + , subq_11.ds__extract_day AS metric_time__extract_day + , subq_11.ds__extract_dow AS metric_time__extract_dow + , subq_11.ds__extract_doy AS metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -461,23 +507,23 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_10 - ) subq_11 - ) subq_12 + ) subq_11 + ) subq_12 + ) subq_13 ON ( - subq_9.user = subq_12.user + subq_10.user = subq_13.user ) AND ( - (subq_9.ds__day <= subq_12.ds__day) + (subq_10.ds__day <= subq_13.ds__day) ) - ) subq_13 - ) subq_14 - ) subq_15 + ) subq_14 + ) subq_15 + ) subq_16 GROUP BY visit__referrer_id - ) subq_16 + ) subq_17 ON - subq_5.visit__referrer_id = subq_16.visit__referrer_id + subq_5.visit__referrer_id = subq_17.visit__referrer_id GROUP BY visit__referrer_id -) subq_17 +) subq_18 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_time_constraint__plan0_optimized.sql index 5c2bdafafd..26dd0198a7 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_time_constraint__plan0_optimized.sql @@ -5,9 +5,9 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) AS visit__referrer_id - , MAX(subq_23.visits) AS visits - , MAX(subq_34.buys) AS buys + COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) AS visit__referrer_id + , MAX(subq_24.visits) AS visits + , MAX(subq_36.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -24,11 +24,11 @@ FROM ( , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 WHERE DATETIME_TRUNC(ds, day) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_21 + ) subq_22 WHERE visit__referrer_id = 'ref_id_01' GROUP BY visit__referrer_id - ) subq_23 + ) subq_24 FULL OUTER JOIN ( -- Find conversions for user within the range of INF -- Pass Only Elements: ['buys', 'visit__referrer_id'] @@ -39,53 +39,62 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_27.visits) OVER ( + FIRST_VALUE(subq_29.visits) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_27.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_29.visit__referrer_id) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_27.ds__day) OVER ( + , FIRST_VALUE(subq_29.ds__day) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_27.user) OVER ( + , FIRST_VALUE(subq_29.user) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_30.mf_internal_uuid AS mf_internal_uuid - , subq_30.buys AS buys + , subq_32.mf_internal_uuid AS mf_internal_uuid + , subq_32.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] SELECT - DATETIME_TRUNC(ds, day) AS ds__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - WHERE DATETIME_TRUNC(ds, day) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_27 + ds__day + , subq_27.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + SELECT + DATETIME_TRUNC(ds, day) AS ds__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + WHERE DATETIME_TRUNC(ds, day) BETWEEN '2020-01-01' AND '2020-01-02' + ) subq_27 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_29 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -96,19 +105,19 @@ FROM ( , 1 AS buys , GENERATE_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_30 + ) subq_32 ON ( - subq_27.user = subq_30.user + subq_29.user = subq_32.user ) AND ( - (subq_27.ds__day <= subq_30.ds__day) + (subq_29.ds__day <= subq_32.ds__day) ) - ) subq_31 + ) subq_33 GROUP BY visit__referrer_id - ) subq_34 + ) subq_36 ON - subq_23.visit__referrer_id = subq_34.visit__referrer_id + subq_24.visit__referrer_id = subq_36.visit__referrer_id GROUP BY visit__referrer_id -) subq_35 +) subq_37 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_window__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_window__plan0.sql index e624deff37..3a029448fe 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_window__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_window__plan0.sql @@ -1,13 +1,13 @@ -- Compute Metrics via Expressions SELECT - subq_15.metric_time__day - , CAST(subq_15.buys AS FLOAT64) / CAST(NULLIF(subq_15.visits, 0) AS FLOAT64) AS visit_buy_conversion_rate_7days + subq_16.metric_time__day + , CAST(subq_16.buys AS FLOAT64) / CAST(NULLIF(subq_16.visits, 0) AS FLOAT64) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) AS metric_time__day + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) AS metric_time__day , MAX(subq_4.visits) AS visits - , MAX(subq_14.buys) AS buys + , MAX(subq_15.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -112,231 +112,277 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_13.metric_time__day - , SUM(subq_13.buys) AS buys + subq_14.metric_time__day + , SUM(subq_14.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'metric_time__day'] SELECT - subq_12.metric_time__day - , subq_12.buys + subq_13.metric_time__day + , subq_13.buys FROM ( -- Find conversions for user within the range of 7 day SELECT - subq_11.ds__day - , subq_11.metric_time__day - , subq_11.user - , subq_11.buys - , subq_11.visits + subq_12.ds__day + , subq_12.metric_time__day + , subq_12.user + , subq_12.buys + , subq_12.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_7.visits) OVER ( + FIRST_VALUE(subq_8.visits) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_7.ds__day) OVER ( + , FIRST_VALUE(subq_8.ds__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_7.metric_time__day) OVER ( + , FIRST_VALUE(subq_8.metric_time__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_7.user) OVER ( + , FIRST_VALUE(subq_8.user) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_10.mf_internal_uuid AS mf_internal_uuid - , subq_10.buys AS buys + , subq_11.mf_internal_uuid AS mf_internal_uuid + , subq_11.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_6.ds__day - , subq_6.metric_time__day - , subq_6.user - , subq_6.visits + subq_7.ds__day + , subq_7.metric_time__day + , subq_7.user + , subq_7.visits FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE SELECT - subq_5.ds__day - , subq_5.ds__week - , subq_5.ds__month - , subq_5.ds__quarter - , subq_5.ds__year - , subq_5.ds__extract_year - , subq_5.ds__extract_quarter - , subq_5.ds__extract_month - , subq_5.ds__extract_day - , subq_5.ds__extract_dow - , subq_5.ds__extract_doy - , subq_5.visit__ds__day - , subq_5.visit__ds__week - , subq_5.visit__ds__month - , subq_5.visit__ds__quarter - , subq_5.visit__ds__year - , subq_5.visit__ds__extract_year - , subq_5.visit__ds__extract_quarter - , subq_5.visit__ds__extract_month - , subq_5.visit__ds__extract_day - , subq_5.visit__ds__extract_dow - , subq_5.visit__ds__extract_doy - , subq_5.ds__day AS metric_time__day - , subq_5.ds__week AS metric_time__week - , subq_5.ds__month AS metric_time__month - , subq_5.ds__quarter AS metric_time__quarter - , subq_5.ds__year AS metric_time__year - , subq_5.ds__extract_year AS metric_time__extract_year - , subq_5.ds__extract_quarter AS metric_time__extract_quarter - , subq_5.ds__extract_month AS metric_time__extract_month - , subq_5.ds__extract_day AS metric_time__extract_day - , subq_5.ds__extract_dow AS metric_time__extract_dow - , subq_5.ds__extract_doy AS metric_time__extract_doy - , subq_5.user - , subq_5.session - , subq_5.visit__user - , subq_5.visit__session - , subq_5.referrer_id - , subq_5.visit__referrer_id - , subq_5.visits - , subq_5.visitors + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.metric_time__day + , subq_6.metric_time__week + , subq_6.metric_time__month + , subq_6.metric_time__quarter + , subq_6.metric_time__year + , subq_6.metric_time__extract_year + , subq_6.metric_time__extract_quarter + , subq_6.metric_time__extract_month + , subq_6.metric_time__extract_day + , subq_6.metric_time__extract_dow + , subq_6.metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS ds__day - , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS ds__week - , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS ds__month - , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS ds__quarter - , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS ds__extract_dow - , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS visit__ds__day - , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS visit__ds__week - , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS visit__ds__month - , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS visit__ds__quarter - , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS visit__ds__extract_dow - , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_5 - ) subq_6 - ) subq_7 + subq_5.ds__day + , subq_5.ds__week + , subq_5.ds__month + , subq_5.ds__quarter + , subq_5.ds__year + , subq_5.ds__extract_year + , subq_5.ds__extract_quarter + , subq_5.ds__extract_month + , subq_5.ds__extract_day + , subq_5.ds__extract_dow + , subq_5.ds__extract_doy + , subq_5.visit__ds__day + , subq_5.visit__ds__week + , subq_5.visit__ds__month + , subq_5.visit__ds__quarter + , subq_5.visit__ds__year + , subq_5.visit__ds__extract_year + , subq_5.visit__ds__extract_quarter + , subq_5.visit__ds__extract_month + , subq_5.visit__ds__extract_day + , subq_5.visit__ds__extract_dow + , subq_5.visit__ds__extract_doy + , subq_5.ds__day AS metric_time__day + , subq_5.ds__week AS metric_time__week + , subq_5.ds__month AS metric_time__month + , subq_5.ds__quarter AS metric_time__quarter + , subq_5.ds__year AS metric_time__year + , subq_5.ds__extract_year AS metric_time__extract_year + , subq_5.ds__extract_quarter AS metric_time__extract_quarter + , subq_5.ds__extract_month AS metric_time__extract_month + , subq_5.ds__extract_day AS metric_time__extract_day + , subq_5.ds__extract_dow AS metric_time__extract_dow + , subq_5.ds__extract_doy AS metric_time__extract_doy + , subq_5.user + , subq_5.session + , subq_5.visit__user + , subq_5.visit__session + , subq_5.referrer_id + , subq_5.visit__referrer_id + , subq_5.visits + , subq_5.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS ds__day + , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS ds__week + , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS ds__month + , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS ds__quarter + , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS visit__ds__day + , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS visit__ds__week + , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS visit__ds__month + , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS visit__ds__quarter + , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS visit__ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_5 + ) subq_6 + WHERE metric_time__day = '2020-01-01' + ) subq_7 + ) subq_8 INNER JOIN ( -- Add column with generated UUID SELECT - subq_9.ds__day - , subq_9.ds__week - , subq_9.ds__month - , subq_9.ds__quarter - , subq_9.ds__year - , subq_9.ds__extract_year - , subq_9.ds__extract_quarter - , subq_9.ds__extract_month - , subq_9.ds__extract_day - , subq_9.ds__extract_dow - , subq_9.ds__extract_doy - , subq_9.buy__ds__day - , subq_9.buy__ds__week - , subq_9.buy__ds__month - , subq_9.buy__ds__quarter - , subq_9.buy__ds__year - , subq_9.buy__ds__extract_year - , subq_9.buy__ds__extract_quarter - , subq_9.buy__ds__extract_month - , subq_9.buy__ds__extract_day - , subq_9.buy__ds__extract_dow - , subq_9.buy__ds__extract_doy - , subq_9.metric_time__day - , subq_9.metric_time__week - , subq_9.metric_time__month - , subq_9.metric_time__quarter - , subq_9.metric_time__year - , subq_9.metric_time__extract_year - , subq_9.metric_time__extract_quarter - , subq_9.metric_time__extract_month - , subq_9.metric_time__extract_day - , subq_9.metric_time__extract_dow - , subq_9.metric_time__extract_doy - , subq_9.user - , subq_9.session_id - , subq_9.buy__user - , subq_9.buy__session_id - , subq_9.buys - , subq_9.buyers + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.metric_time__day + , subq_10.metric_time__week + , subq_10.metric_time__month + , subq_10.metric_time__quarter + , subq_10.metric_time__year + , subq_10.metric_time__extract_year + , subq_10.metric_time__extract_quarter + , subq_10.metric_time__extract_month + , subq_10.metric_time__extract_day + , subq_10.metric_time__extract_dow + , subq_10.metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers , GENERATE_UUID() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_8.ds__day - , subq_8.ds__week - , subq_8.ds__month - , subq_8.ds__quarter - , subq_8.ds__year - , subq_8.ds__extract_year - , subq_8.ds__extract_quarter - , subq_8.ds__extract_month - , subq_8.ds__extract_day - , subq_8.ds__extract_dow - , subq_8.ds__extract_doy - , subq_8.buy__ds__day - , subq_8.buy__ds__week - , subq_8.buy__ds__month - , subq_8.buy__ds__quarter - , subq_8.buy__ds__year - , subq_8.buy__ds__extract_year - , subq_8.buy__ds__extract_quarter - , subq_8.buy__ds__extract_month - , subq_8.buy__ds__extract_day - , subq_8.buy__ds__extract_dow - , subq_8.buy__ds__extract_doy - , subq_8.ds__day AS metric_time__day - , subq_8.ds__week AS metric_time__week - , subq_8.ds__month AS metric_time__month - , subq_8.ds__quarter AS metric_time__quarter - , subq_8.ds__year AS metric_time__year - , subq_8.ds__extract_year AS metric_time__extract_year - , subq_8.ds__extract_quarter AS metric_time__extract_quarter - , subq_8.ds__extract_month AS metric_time__extract_month - , subq_8.ds__extract_day AS metric_time__extract_day - , subq_8.ds__extract_dow AS metric_time__extract_dow - , subq_8.ds__extract_doy AS metric_time__extract_doy - , subq_8.user - , subq_8.session_id - , subq_8.buy__user - , subq_8.buy__session_id - , subq_8.buys - , subq_8.buyers + subq_9.ds__day + , subq_9.ds__week + , subq_9.ds__month + , subq_9.ds__quarter + , subq_9.ds__year + , subq_9.ds__extract_year + , subq_9.ds__extract_quarter + , subq_9.ds__extract_month + , subq_9.ds__extract_day + , subq_9.ds__extract_dow + , subq_9.ds__extract_doy + , subq_9.buy__ds__day + , subq_9.buy__ds__week + , subq_9.buy__ds__month + , subq_9.buy__ds__quarter + , subq_9.buy__ds__year + , subq_9.buy__ds__extract_year + , subq_9.buy__ds__extract_quarter + , subq_9.buy__ds__extract_month + , subq_9.buy__ds__extract_day + , subq_9.buy__ds__extract_dow + , subq_9.buy__ds__extract_doy + , subq_9.ds__day AS metric_time__day + , subq_9.ds__week AS metric_time__week + , subq_9.ds__month AS metric_time__month + , subq_9.ds__quarter AS metric_time__quarter + , subq_9.ds__year AS metric_time__year + , subq_9.ds__extract_year AS metric_time__extract_year + , subq_9.ds__extract_quarter AS metric_time__extract_quarter + , subq_9.ds__extract_month AS metric_time__extract_month + , subq_9.ds__extract_day AS metric_time__extract_day + , subq_9.ds__extract_dow AS metric_time__extract_dow + , subq_9.ds__extract_doy AS metric_time__extract_doy + , subq_9.user + , subq_9.session_id + , subq_9.buy__user + , subq_9.buy__session_id + , subq_9.buys + , subq_9.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -369,27 +415,27 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_8 - ) subq_9 - ) subq_10 + ) subq_9 + ) subq_10 + ) subq_11 ON ( - subq_7.user = subq_10.user + subq_8.user = subq_11.user ) AND ( ( - subq_7.ds__day <= subq_10.ds__day + subq_8.ds__day <= subq_11.ds__day ) AND ( - subq_7.ds__day > DATE_SUB(CAST(subq_10.ds__day AS DATETIME), INTERVAL 7 day) + subq_8.ds__day > DATE_SUB(CAST(subq_11.ds__day AS DATETIME), INTERVAL 7 day) ) ) - ) subq_11 - ) subq_12 - ) subq_13 + ) subq_12 + ) subq_13 + ) subq_14 GROUP BY metric_time__day - ) subq_14 + ) subq_15 ON - subq_4.metric_time__day = subq_14.metric_time__day + subq_4.metric_time__day = subq_15.metric_time__day GROUP BY metric_time__day -) subq_15 +) subq_16 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_window__plan0_optimized.sql index 7060991a4d..30e4dc3739 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_window__plan0_optimized.sql @@ -5,9 +5,9 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_20.visits) AS visits - , MAX(subq_30.buys) AS buys + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , MAX(subq_21.visits) AS visits + , MAX(subq_32.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -22,11 +22,11 @@ FROM ( DATETIME_TRUNC(ds, day) AS metric_time__day , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_18 + ) subq_19 WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__day - ) subq_20 + ) subq_21 FULL OUTER JOIN ( -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'metric_time__day'] @@ -37,51 +37,60 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_23.visits) OVER ( + FIRST_VALUE(subq_25.visits) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_23.ds__day) OVER ( + , FIRST_VALUE(subq_25.ds__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_23.metric_time__day) OVER ( + , FIRST_VALUE(subq_25.metric_time__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_23.user) OVER ( + , FIRST_VALUE(subq_25.user) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_26.mf_internal_uuid AS mf_internal_uuid - , subq_26.buys AS buys + , subq_28.mf_internal_uuid AS mf_internal_uuid + , subq_28.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - DATETIME_TRUNC(ds, day) AS ds__day - , DATETIME_TRUNC(ds, day) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + ds__day + , metric_time__day + , subq_23.user + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS ds__day + , DATETIME_TRUNC(ds, day) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_23 + WHERE metric_time__day = '2020-01-01' + ) subq_25 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -92,23 +101,23 @@ FROM ( , 1 AS buys , GENERATE_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_26 + ) subq_28 ON ( - subq_23.user = subq_26.user + subq_25.user = subq_28.user ) AND ( ( - subq_23.ds__day <= subq_26.ds__day + subq_25.ds__day <= subq_28.ds__day ) AND ( - subq_23.ds__day > DATE_SUB(CAST(subq_26.ds__day AS DATETIME), INTERVAL 7 day) + subq_25.ds__day > DATE_SUB(CAST(subq_28.ds__day AS DATETIME), INTERVAL 7 day) ) ) - ) subq_27 + ) subq_29 GROUP BY metric_time__day - ) subq_30 + ) subq_32 ON - subq_20.metric_time__day = subq_30.metric_time__day + subq_21.metric_time__day = subq_32.metric_time__day GROUP BY metric_time__day -) subq_31 +) subq_33 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_window_and_time_constraint__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_window_and_time_constraint__plan0.sql index fb5c8699ff..ff2787aa41 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_window_and_time_constraint__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_window_and_time_constraint__plan0.sql @@ -1,15 +1,15 @@ -- Compute Metrics via Expressions SELECT - subq_17.metric_time__day - , subq_17.visit__referrer_id - , CAST(subq_17.buys AS FLOAT64) / CAST(NULLIF(subq_17.visits, 0) AS FLOAT64) AS visit_buy_conversion_rate_7days + subq_18.metric_time__day + , subq_18.visit__referrer_id + , CAST(subq_18.buys AS FLOAT64) / CAST(NULLIF(subq_18.visits, 0) AS FLOAT64) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_5.metric_time__day, subq_16.metric_time__day) AS metric_time__day - , COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_5.metric_time__day, subq_17.metric_time__day) AS metric_time__day + , COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) AS visit__referrer_id , MAX(subq_5.visits) AS visits - , MAX(subq_16.buys) AS buys + , MAX(subq_17.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -164,289 +164,335 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_15.metric_time__day - , subq_15.visit__referrer_id - , SUM(subq_15.buys) AS buys + subq_16.metric_time__day + , subq_16.visit__referrer_id + , SUM(subq_16.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] SELECT - subq_14.metric_time__day - , subq_14.visit__referrer_id - , subq_14.buys + subq_15.metric_time__day + , subq_15.visit__referrer_id + , subq_15.buys FROM ( -- Find conversions for user within the range of 7 day SELECT - subq_13.ds__day - , subq_13.metric_time__day - , subq_13.user - , subq_13.visit__referrer_id - , subq_13.buys - , subq_13.visits + subq_14.ds__day + , subq_14.metric_time__day + , subq_14.user + , subq_14.visit__referrer_id + , subq_14.buys + , subq_14.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_9.visits) OVER ( + FIRST_VALUE(subq_10.visits) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_9.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_10.visit__referrer_id) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_9.ds__day) OVER ( + , FIRST_VALUE(subq_10.ds__day) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_9.metric_time__day) OVER ( + , FIRST_VALUE(subq_10.metric_time__day) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_9.user) OVER ( + , FIRST_VALUE(subq_10.user) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_12.mf_internal_uuid AS mf_internal_uuid - , subq_12.buys AS buys + , subq_13.mf_internal_uuid AS mf_internal_uuid + , subq_13.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_8.ds__day - , subq_8.metric_time__day - , subq_8.user - , subq_8.visit__referrer_id - , subq_8.visits + subq_9.ds__day + , subq_9.metric_time__day + , subq_9.user + , subq_9.visit__referrer_id + , subq_9.visits FROM ( - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE SELECT - subq_7.ds__day - , subq_7.ds__week - , subq_7.ds__month - , subq_7.ds__quarter - , subq_7.ds__year - , subq_7.ds__extract_year - , subq_7.ds__extract_quarter - , subq_7.ds__extract_month - , subq_7.ds__extract_day - , subq_7.ds__extract_dow - , subq_7.ds__extract_doy - , subq_7.visit__ds__day - , subq_7.visit__ds__week - , subq_7.visit__ds__month - , subq_7.visit__ds__quarter - , subq_7.visit__ds__year - , subq_7.visit__ds__extract_year - , subq_7.visit__ds__extract_quarter - , subq_7.visit__ds__extract_month - , subq_7.visit__ds__extract_day - , subq_7.visit__ds__extract_dow - , subq_7.visit__ds__extract_doy - , subq_7.metric_time__day - , subq_7.metric_time__week - , subq_7.metric_time__month - , subq_7.metric_time__quarter - , subq_7.metric_time__year - , subq_7.metric_time__extract_year - , subq_7.metric_time__extract_quarter - , subq_7.metric_time__extract_month - , subq_7.metric_time__extract_day - , subq_7.metric_time__extract_dow - , subq_7.metric_time__extract_doy - , subq_7.user - , subq_7.session - , subq_7.visit__user - , subq_7.visit__session - , subq_7.referrer_id - , subq_7.visit__referrer_id - , subq_7.visits - , subq_7.visitors + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.visit__ds__day + , subq_8.visit__ds__week + , subq_8.visit__ds__month + , subq_8.visit__ds__quarter + , subq_8.visit__ds__year + , subq_8.visit__ds__extract_year + , subq_8.visit__ds__extract_quarter + , subq_8.visit__ds__extract_month + , subq_8.visit__ds__extract_day + , subq_8.visit__ds__extract_dow + , subq_8.visit__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session + , subq_8.visit__user + , subq_8.visit__session + , subq_8.referrer_id + , subq_8.visit__referrer_id + , subq_8.visits + , subq_8.visitors FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] SELECT - subq_6.ds__day - , subq_6.ds__week - , subq_6.ds__month - , subq_6.ds__quarter - , subq_6.ds__year - , subq_6.ds__extract_year - , subq_6.ds__extract_quarter - , subq_6.ds__extract_month - , subq_6.ds__extract_day - , subq_6.ds__extract_dow - , subq_6.ds__extract_doy - , subq_6.visit__ds__day - , subq_6.visit__ds__week - , subq_6.visit__ds__month - , subq_6.visit__ds__quarter - , subq_6.visit__ds__year - , subq_6.visit__ds__extract_year - , subq_6.visit__ds__extract_quarter - , subq_6.visit__ds__extract_month - , subq_6.visit__ds__extract_day - , subq_6.visit__ds__extract_dow - , subq_6.visit__ds__extract_doy - , subq_6.ds__day AS metric_time__day - , subq_6.ds__week AS metric_time__week - , subq_6.ds__month AS metric_time__month - , subq_6.ds__quarter AS metric_time__quarter - , subq_6.ds__year AS metric_time__year - , subq_6.ds__extract_year AS metric_time__extract_year - , subq_6.ds__extract_quarter AS metric_time__extract_quarter - , subq_6.ds__extract_month AS metric_time__extract_month - , subq_6.ds__extract_day AS metric_time__extract_day - , subq_6.ds__extract_dow AS metric_time__extract_dow - , subq_6.ds__extract_doy AS metric_time__extract_doy - , subq_6.user - , subq_6.session - , subq_6.visit__user - , subq_6.visit__session - , subq_6.referrer_id - , subq_6.visit__referrer_id - , subq_6.visits - , subq_6.visitors + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS ds__day - , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS ds__week - , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS ds__month - , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS ds__quarter - , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS ds__extract_dow - , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS visit__ds__day - , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS visit__ds__week - , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS visit__ds__month - , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS visit__ds__quarter - , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS visit__ds__extract_dow - , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_6 - ) subq_7 - WHERE subq_7.metric_time__day BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_8 - ) subq_9 + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS ds__day + , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS ds__week + , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS ds__month + , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS ds__quarter + , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS visit__ds__day + , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS visit__ds__week + , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS visit__ds__month + , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS visit__ds__quarter + , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS visit__ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE subq_7.metric_time__day BETWEEN '2020-01-01' AND '2020-01-02' + ) subq_8 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_9 + ) subq_10 INNER JOIN ( -- Add column with generated UUID SELECT - subq_11.ds__day - , subq_11.ds__week - , subq_11.ds__month - , subq_11.ds__quarter - , subq_11.ds__year - , subq_11.ds__extract_year - , subq_11.ds__extract_quarter - , subq_11.ds__extract_month - , subq_11.ds__extract_day - , subq_11.ds__extract_dow - , subq_11.ds__extract_doy - , subq_11.buy__ds__day - , subq_11.buy__ds__week - , subq_11.buy__ds__month - , subq_11.buy__ds__quarter - , subq_11.buy__ds__year - , subq_11.buy__ds__extract_year - , subq_11.buy__ds__extract_quarter - , subq_11.buy__ds__extract_month - , subq_11.buy__ds__extract_day - , subq_11.buy__ds__extract_dow - , subq_11.buy__ds__extract_doy - , subq_11.metric_time__day - , subq_11.metric_time__week - , subq_11.metric_time__month - , subq_11.metric_time__quarter - , subq_11.metric_time__year - , subq_11.metric_time__extract_year - , subq_11.metric_time__extract_quarter - , subq_11.metric_time__extract_month - , subq_11.metric_time__extract_day - , subq_11.metric_time__extract_dow - , subq_11.metric_time__extract_doy - , subq_11.user - , subq_11.session_id - , subq_11.buy__user - , subq_11.buy__session_id - , subq_11.buys - , subq_11.buyers + subq_12.ds__day + , subq_12.ds__week + , subq_12.ds__month + , subq_12.ds__quarter + , subq_12.ds__year + , subq_12.ds__extract_year + , subq_12.ds__extract_quarter + , subq_12.ds__extract_month + , subq_12.ds__extract_day + , subq_12.ds__extract_dow + , subq_12.ds__extract_doy + , subq_12.buy__ds__day + , subq_12.buy__ds__week + , subq_12.buy__ds__month + , subq_12.buy__ds__quarter + , subq_12.buy__ds__year + , subq_12.buy__ds__extract_year + , subq_12.buy__ds__extract_quarter + , subq_12.buy__ds__extract_month + , subq_12.buy__ds__extract_day + , subq_12.buy__ds__extract_dow + , subq_12.buy__ds__extract_doy + , subq_12.metric_time__day + , subq_12.metric_time__week + , subq_12.metric_time__month + , subq_12.metric_time__quarter + , subq_12.metric_time__year + , subq_12.metric_time__extract_year + , subq_12.metric_time__extract_quarter + , subq_12.metric_time__extract_month + , subq_12.metric_time__extract_day + , subq_12.metric_time__extract_dow + , subq_12.metric_time__extract_doy + , subq_12.user + , subq_12.session_id + , subq_12.buy__user + , subq_12.buy__session_id + , subq_12.buys + , subq_12.buyers , GENERATE_UUID() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_10.ds__day - , subq_10.ds__week - , subq_10.ds__month - , subq_10.ds__quarter - , subq_10.ds__year - , subq_10.ds__extract_year - , subq_10.ds__extract_quarter - , subq_10.ds__extract_month - , subq_10.ds__extract_day - , subq_10.ds__extract_dow - , subq_10.ds__extract_doy - , subq_10.buy__ds__day - , subq_10.buy__ds__week - , subq_10.buy__ds__month - , subq_10.buy__ds__quarter - , subq_10.buy__ds__year - , subq_10.buy__ds__extract_year - , subq_10.buy__ds__extract_quarter - , subq_10.buy__ds__extract_month - , subq_10.buy__ds__extract_day - , subq_10.buy__ds__extract_dow - , subq_10.buy__ds__extract_doy - , subq_10.ds__day AS metric_time__day - , subq_10.ds__week AS metric_time__week - , subq_10.ds__month AS metric_time__month - , subq_10.ds__quarter AS metric_time__quarter - , subq_10.ds__year AS metric_time__year - , subq_10.ds__extract_year AS metric_time__extract_year - , subq_10.ds__extract_quarter AS metric_time__extract_quarter - , subq_10.ds__extract_month AS metric_time__extract_month - , subq_10.ds__extract_day AS metric_time__extract_day - , subq_10.ds__extract_dow AS metric_time__extract_dow - , subq_10.ds__extract_doy AS metric_time__extract_doy - , subq_10.user - , subq_10.session_id - , subq_10.buy__user - , subq_10.buy__session_id - , subq_10.buys - , subq_10.buyers + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.ds__day AS metric_time__day + , subq_11.ds__week AS metric_time__week + , subq_11.ds__month AS metric_time__month + , subq_11.ds__quarter AS metric_time__quarter + , subq_11.ds__year AS metric_time__year + , subq_11.ds__extract_year AS metric_time__extract_year + , subq_11.ds__extract_quarter AS metric_time__extract_quarter + , subq_11.ds__extract_month AS metric_time__extract_month + , subq_11.ds__extract_day AS metric_time__extract_day + , subq_11.ds__extract_dow AS metric_time__extract_dow + , subq_11.ds__extract_doy AS metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -479,33 +525,33 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_10 - ) subq_11 - ) subq_12 + ) subq_11 + ) subq_12 + ) subq_13 ON ( - subq_9.user = subq_12.user + subq_10.user = subq_13.user ) AND ( ( - subq_9.ds__day <= subq_12.ds__day + subq_10.ds__day <= subq_13.ds__day ) AND ( - subq_9.ds__day > DATE_SUB(CAST(subq_12.ds__day AS DATETIME), INTERVAL 7 day) + subq_10.ds__day > DATE_SUB(CAST(subq_13.ds__day AS DATETIME), INTERVAL 7 day) ) ) - ) subq_13 - ) subq_14 - ) subq_15 + ) subq_14 + ) subq_15 + ) subq_16 GROUP BY metric_time__day , visit__referrer_id - ) subq_16 + ) subq_17 ON ( - subq_5.visit__referrer_id = subq_16.visit__referrer_id + subq_5.visit__referrer_id = subq_17.visit__referrer_id ) AND ( - subq_5.metric_time__day = subq_16.metric_time__day + subq_5.metric_time__day = subq_17.metric_time__day ) GROUP BY metric_time__day , visit__referrer_id -) subq_17 +) subq_18 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql index d478aab86e..25a27b230b 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql @@ -6,10 +6,10 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_23.metric_time__day, subq_34.metric_time__day) AS metric_time__day - , COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) AS visit__referrer_id - , MAX(subq_23.visits) AS visits - , MAX(subq_34.buys) AS buys + COALESCE(subq_24.metric_time__day, subq_36.metric_time__day) AS metric_time__day + , COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) AS visit__referrer_id + , MAX(subq_24.visits) AS visits + , MAX(subq_36.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -28,12 +28,12 @@ FROM ( , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 WHERE DATETIME_TRUNC(ds, day) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_21 + ) subq_22 WHERE visit__referrer_id = 'ref_id_01' GROUP BY metric_time__day , visit__referrer_id - ) subq_23 + ) subq_24 FULL OUTER JOIN ( -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] @@ -45,62 +45,72 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_27.visits) OVER ( + FIRST_VALUE(subq_29.visits) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_27.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_29.visit__referrer_id) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_27.ds__day) OVER ( + , FIRST_VALUE(subq_29.ds__day) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_27.metric_time__day) OVER ( + , FIRST_VALUE(subq_29.metric_time__day) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_27.user) OVER ( + , FIRST_VALUE(subq_29.user) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_30.mf_internal_uuid AS mf_internal_uuid - , subq_30.buys AS buys + , subq_32.mf_internal_uuid AS mf_internal_uuid + , subq_32.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - DATETIME_TRUNC(ds, day) AS ds__day - , 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 - WHERE DATETIME_TRUNC(ds, day) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_27 + ds__day + , metric_time__day + , subq_27.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + SELECT + DATETIME_TRUNC(ds, day) AS ds__day + , 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 + WHERE DATETIME_TRUNC(ds, day) BETWEEN '2020-01-01' AND '2020-01-02' + ) subq_27 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_29 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -111,29 +121,29 @@ FROM ( , 1 AS buys , GENERATE_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_30 + ) subq_32 ON ( - subq_27.user = subq_30.user + subq_29.user = subq_32.user ) AND ( ( - subq_27.ds__day <= subq_30.ds__day + subq_29.ds__day <= subq_32.ds__day ) AND ( - subq_27.ds__day > DATE_SUB(CAST(subq_30.ds__day AS DATETIME), INTERVAL 7 day) + subq_29.ds__day > DATE_SUB(CAST(subq_32.ds__day AS DATETIME), INTERVAL 7 day) ) ) - ) subq_31 + ) subq_33 GROUP BY metric_time__day , visit__referrer_id - ) subq_34 + ) subq_36 ON ( - subq_23.visit__referrer_id = subq_34.visit__referrer_id + subq_24.visit__referrer_id = subq_36.visit__referrer_id ) AND ( - subq_23.metric_time__day = subq_34.metric_time__day + subq_24.metric_time__day = subq_36.metric_time__day ) GROUP BY metric_time__day , visit__referrer_id -) subq_35 +) subq_37 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric__plan0.sql index 9e0429d976..502bd995ef 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric__plan0.sql @@ -1,13 +1,13 @@ -- Compute Metrics via Expressions SELECT - subq_15.metric_time__day - , CAST(subq_15.buys AS DOUBLE) / CAST(NULLIF(subq_15.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate + subq_16.metric_time__day + , CAST(subq_16.buys AS DOUBLE) / CAST(NULLIF(subq_16.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) AS metric_time__day + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) AS metric_time__day , MAX(subq_4.visits) AS visits - , MAX(subq_14.buys) AS buys + , MAX(subq_15.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -112,231 +112,277 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_13.metric_time__day - , SUM(subq_13.buys) AS buys + subq_14.metric_time__day + , SUM(subq_14.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'metric_time__day'] SELECT - subq_12.metric_time__day - , subq_12.buys + subq_13.metric_time__day + , subq_13.buys FROM ( -- Find conversions for user within the range of INF SELECT - subq_11.ds__day - , subq_11.metric_time__day - , subq_11.user - , subq_11.buys - , subq_11.visits + subq_12.ds__day + , subq_12.metric_time__day + , subq_12.user + , subq_12.buys + , subq_12.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_7.visits) OVER ( + FIRST_VALUE(subq_8.visits) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_7.ds__day) OVER ( + , FIRST_VALUE(subq_8.ds__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_7.metric_time__day) OVER ( + , FIRST_VALUE(subq_8.metric_time__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_7.user) OVER ( + , FIRST_VALUE(subq_8.user) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_10.mf_internal_uuid AS mf_internal_uuid - , subq_10.buys AS buys + , subq_11.mf_internal_uuid AS mf_internal_uuid + , subq_11.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_6.ds__day - , subq_6.metric_time__day - , subq_6.user - , subq_6.visits + subq_7.ds__day + , subq_7.metric_time__day + , subq_7.user + , subq_7.visits FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE SELECT - subq_5.ds__day - , subq_5.ds__week - , subq_5.ds__month - , subq_5.ds__quarter - , subq_5.ds__year - , subq_5.ds__extract_year - , subq_5.ds__extract_quarter - , subq_5.ds__extract_month - , subq_5.ds__extract_day - , subq_5.ds__extract_dow - , subq_5.ds__extract_doy - , subq_5.visit__ds__day - , subq_5.visit__ds__week - , subq_5.visit__ds__month - , subq_5.visit__ds__quarter - , subq_5.visit__ds__year - , subq_5.visit__ds__extract_year - , subq_5.visit__ds__extract_quarter - , subq_5.visit__ds__extract_month - , subq_5.visit__ds__extract_day - , subq_5.visit__ds__extract_dow - , subq_5.visit__ds__extract_doy - , subq_5.ds__day AS metric_time__day - , subq_5.ds__week AS metric_time__week - , subq_5.ds__month AS metric_time__month - , subq_5.ds__quarter AS metric_time__quarter - , subq_5.ds__year AS metric_time__year - , subq_5.ds__extract_year AS metric_time__extract_year - , subq_5.ds__extract_quarter AS metric_time__extract_quarter - , subq_5.ds__extract_month AS metric_time__extract_month - , subq_5.ds__extract_day AS metric_time__extract_day - , subq_5.ds__extract_dow AS metric_time__extract_dow - , subq_5.ds__extract_doy AS metric_time__extract_doy - , subq_5.user - , subq_5.session - , subq_5.visit__user - , subq_5.visit__session - , subq_5.referrer_id - , subq_5.visit__referrer_id - , subq_5.visits - , subq_5.visitors + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.metric_time__day + , subq_6.metric_time__week + , subq_6.metric_time__month + , subq_6.metric_time__quarter + , subq_6.metric_time__year + , subq_6.metric_time__extract_year + , subq_6.metric_time__extract_quarter + , subq_6.metric_time__extract_month + , subq_6.metric_time__extract_day + , subq_6.metric_time__extract_dow + , subq_6.metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_5 - ) subq_6 - ) subq_7 + subq_5.ds__day + , subq_5.ds__week + , subq_5.ds__month + , subq_5.ds__quarter + , subq_5.ds__year + , subq_5.ds__extract_year + , subq_5.ds__extract_quarter + , subq_5.ds__extract_month + , subq_5.ds__extract_day + , subq_5.ds__extract_dow + , subq_5.ds__extract_doy + , subq_5.visit__ds__day + , subq_5.visit__ds__week + , subq_5.visit__ds__month + , subq_5.visit__ds__quarter + , subq_5.visit__ds__year + , subq_5.visit__ds__extract_year + , subq_5.visit__ds__extract_quarter + , subq_5.visit__ds__extract_month + , subq_5.visit__ds__extract_day + , subq_5.visit__ds__extract_dow + , subq_5.visit__ds__extract_doy + , subq_5.ds__day AS metric_time__day + , subq_5.ds__week AS metric_time__week + , subq_5.ds__month AS metric_time__month + , subq_5.ds__quarter AS metric_time__quarter + , subq_5.ds__year AS metric_time__year + , subq_5.ds__extract_year AS metric_time__extract_year + , subq_5.ds__extract_quarter AS metric_time__extract_quarter + , subq_5.ds__extract_month AS metric_time__extract_month + , subq_5.ds__extract_day AS metric_time__extract_day + , subq_5.ds__extract_dow AS metric_time__extract_dow + , subq_5.ds__extract_doy AS metric_time__extract_doy + , subq_5.user + , subq_5.session + , subq_5.visit__user + , subq_5.visit__session + , subq_5.referrer_id + , subq_5.visit__referrer_id + , subq_5.visits + , subq_5.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_5 + ) subq_6 + WHERE metric_time__day = '2020-01-01' + ) subq_7 + ) subq_8 INNER JOIN ( -- Add column with generated UUID SELECT - subq_9.ds__day - , subq_9.ds__week - , subq_9.ds__month - , subq_9.ds__quarter - , subq_9.ds__year - , subq_9.ds__extract_year - , subq_9.ds__extract_quarter - , subq_9.ds__extract_month - , subq_9.ds__extract_day - , subq_9.ds__extract_dow - , subq_9.ds__extract_doy - , subq_9.buy__ds__day - , subq_9.buy__ds__week - , subq_9.buy__ds__month - , subq_9.buy__ds__quarter - , subq_9.buy__ds__year - , subq_9.buy__ds__extract_year - , subq_9.buy__ds__extract_quarter - , subq_9.buy__ds__extract_month - , subq_9.buy__ds__extract_day - , subq_9.buy__ds__extract_dow - , subq_9.buy__ds__extract_doy - , subq_9.metric_time__day - , subq_9.metric_time__week - , subq_9.metric_time__month - , subq_9.metric_time__quarter - , subq_9.metric_time__year - , subq_9.metric_time__extract_year - , subq_9.metric_time__extract_quarter - , subq_9.metric_time__extract_month - , subq_9.metric_time__extract_day - , subq_9.metric_time__extract_dow - , subq_9.metric_time__extract_doy - , subq_9.user - , subq_9.session_id - , subq_9.buy__user - , subq_9.buy__session_id - , subq_9.buys - , subq_9.buyers + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.metric_time__day + , subq_10.metric_time__week + , subq_10.metric_time__month + , subq_10.metric_time__quarter + , subq_10.metric_time__year + , subq_10.metric_time__extract_year + , subq_10.metric_time__extract_quarter + , subq_10.metric_time__extract_month + , subq_10.metric_time__extract_day + , subq_10.metric_time__extract_dow + , subq_10.metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers , UUID() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_8.ds__day - , subq_8.ds__week - , subq_8.ds__month - , subq_8.ds__quarter - , subq_8.ds__year - , subq_8.ds__extract_year - , subq_8.ds__extract_quarter - , subq_8.ds__extract_month - , subq_8.ds__extract_day - , subq_8.ds__extract_dow - , subq_8.ds__extract_doy - , subq_8.buy__ds__day - , subq_8.buy__ds__week - , subq_8.buy__ds__month - , subq_8.buy__ds__quarter - , subq_8.buy__ds__year - , subq_8.buy__ds__extract_year - , subq_8.buy__ds__extract_quarter - , subq_8.buy__ds__extract_month - , subq_8.buy__ds__extract_day - , subq_8.buy__ds__extract_dow - , subq_8.buy__ds__extract_doy - , subq_8.ds__day AS metric_time__day - , subq_8.ds__week AS metric_time__week - , subq_8.ds__month AS metric_time__month - , subq_8.ds__quarter AS metric_time__quarter - , subq_8.ds__year AS metric_time__year - , subq_8.ds__extract_year AS metric_time__extract_year - , subq_8.ds__extract_quarter AS metric_time__extract_quarter - , subq_8.ds__extract_month AS metric_time__extract_month - , subq_8.ds__extract_day AS metric_time__extract_day - , subq_8.ds__extract_dow AS metric_time__extract_dow - , subq_8.ds__extract_doy AS metric_time__extract_doy - , subq_8.user - , subq_8.session_id - , subq_8.buy__user - , subq_8.buy__session_id - , subq_8.buys - , subq_8.buyers + subq_9.ds__day + , subq_9.ds__week + , subq_9.ds__month + , subq_9.ds__quarter + , subq_9.ds__year + , subq_9.ds__extract_year + , subq_9.ds__extract_quarter + , subq_9.ds__extract_month + , subq_9.ds__extract_day + , subq_9.ds__extract_dow + , subq_9.ds__extract_doy + , subq_9.buy__ds__day + , subq_9.buy__ds__week + , subq_9.buy__ds__month + , subq_9.buy__ds__quarter + , subq_9.buy__ds__year + , subq_9.buy__ds__extract_year + , subq_9.buy__ds__extract_quarter + , subq_9.buy__ds__extract_month + , subq_9.buy__ds__extract_day + , subq_9.buy__ds__extract_dow + , subq_9.buy__ds__extract_doy + , subq_9.ds__day AS metric_time__day + , subq_9.ds__week AS metric_time__week + , subq_9.ds__month AS metric_time__month + , subq_9.ds__quarter AS metric_time__quarter + , subq_9.ds__year AS metric_time__year + , subq_9.ds__extract_year AS metric_time__extract_year + , subq_9.ds__extract_quarter AS metric_time__extract_quarter + , subq_9.ds__extract_month AS metric_time__extract_month + , subq_9.ds__extract_day AS metric_time__extract_day + , subq_9.ds__extract_dow AS metric_time__extract_dow + , subq_9.ds__extract_doy AS metric_time__extract_doy + , subq_9.user + , subq_9.session_id + , subq_9.buy__user + , subq_9.buy__session_id + , subq_9.buys + , subq_9.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -369,23 +415,23 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_8 - ) subq_9 - ) subq_10 + ) subq_9 + ) subq_10 + ) subq_11 ON ( - subq_7.user = subq_10.user + subq_8.user = subq_11.user ) AND ( - (subq_7.ds__day <= subq_10.ds__day) + (subq_8.ds__day <= subq_11.ds__day) ) - ) subq_11 - ) subq_12 - ) subq_13 + ) subq_12 + ) subq_13 + ) subq_14 GROUP BY - subq_13.metric_time__day - ) subq_14 + subq_14.metric_time__day + ) subq_15 ON - subq_4.metric_time__day = subq_14.metric_time__day + subq_4.metric_time__day = subq_15.metric_time__day GROUP BY - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) -) subq_15 + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) +) subq_16 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric__plan0_optimized.sql index 2ca2302ef6..59b17b5176 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric__plan0_optimized.sql @@ -5,9 +5,9 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_20.visits) AS visits - , MAX(subq_30.buys) AS buys + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , MAX(subq_21.visits) AS visits + , MAX(subq_32.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -22,11 +22,11 @@ FROM ( DATE_TRUNC('day', ds) AS metric_time__day , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_18 + ) subq_19 WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__day - ) subq_20 + ) subq_21 FULL OUTER JOIN ( -- Find conversions for user within the range of INF -- Pass Only Elements: ['buys', 'metric_time__day'] @@ -37,51 +37,60 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_23.visits) OVER ( + FIRST_VALUE(subq_25.visits) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_23.ds__day) OVER ( + , FIRST_VALUE(subq_25.ds__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_23.metric_time__day) OVER ( + , FIRST_VALUE(subq_25.metric_time__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_23.user) OVER ( + , FIRST_VALUE(subq_25.user) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_26.mf_internal_uuid AS mf_internal_uuid - , subq_26.buys AS buys + , subq_28.mf_internal_uuid AS mf_internal_uuid + , subq_28.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + ds__day + , metric_time__day + , subq_23.user + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_23 + WHERE metric_time__day = '2020-01-01' + ) subq_25 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -92,19 +101,19 @@ FROM ( , 1 AS buys , UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_26 + ) subq_28 ON ( - subq_23.user = subq_26.user + subq_25.user = subq_28.user ) AND ( - (subq_23.ds__day <= subq_26.ds__day) + (subq_25.ds__day <= subq_28.ds__day) ) - ) subq_27 + ) subq_29 GROUP BY metric_time__day - ) subq_30 + ) subq_32 ON - subq_20.metric_time__day = subq_30.metric_time__day + subq_21.metric_time__day = subq_32.metric_time__day GROUP BY - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) -) subq_31 + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) +) subq_33 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_categorical_filter__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_categorical_filter__plan0.sql index dc3f01d3a7..84354d3a94 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_categorical_filter__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_categorical_filter__plan0.sql @@ -1,15 +1,15 @@ -- Compute Metrics via Expressions SELECT - subq_15.metric_time__day - , subq_15.visit__referrer_id - , CAST(subq_15.buys AS DOUBLE) / CAST(NULLIF(subq_15.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate + subq_16.metric_time__day + , subq_16.visit__referrer_id + , CAST(subq_16.buys AS DOUBLE) / CAST(NULLIF(subq_16.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) AS metric_time__day - , COALESCE(subq_4.visit__referrer_id, subq_14.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) AS metric_time__day + , COALESCE(subq_4.visit__referrer_id, subq_15.visit__referrer_id) AS visit__referrer_id , MAX(subq_4.visits) AS visits - , MAX(subq_14.buys) AS buys + , MAX(subq_15.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -118,243 +118,289 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_13.metric_time__day - , subq_13.visit__referrer_id - , SUM(subq_13.buys) AS buys + subq_14.metric_time__day + , subq_14.visit__referrer_id + , SUM(subq_14.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] SELECT - subq_12.metric_time__day - , subq_12.visit__referrer_id - , subq_12.buys + subq_13.metric_time__day + , subq_13.visit__referrer_id + , subq_13.buys FROM ( -- Find conversions for user within the range of INF SELECT - subq_11.ds__day - , subq_11.metric_time__day - , subq_11.user - , subq_11.visit__referrer_id - , subq_11.buys - , subq_11.visits + subq_12.ds__day + , subq_12.metric_time__day + , subq_12.user + , subq_12.visit__referrer_id + , subq_12.buys + , subq_12.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_7.visits) OVER ( + FIRST_VALUE(subq_8.visits) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_7.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_8.visit__referrer_id) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_7.ds__day) OVER ( + , FIRST_VALUE(subq_8.ds__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_7.metric_time__day) OVER ( + , FIRST_VALUE(subq_8.metric_time__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_7.user) OVER ( + , FIRST_VALUE(subq_8.user) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_10.mf_internal_uuid AS mf_internal_uuid - , subq_10.buys AS buys + , subq_11.mf_internal_uuid AS mf_internal_uuid + , subq_11.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_6.ds__day - , subq_6.metric_time__day - , subq_6.user - , subq_6.visit__referrer_id - , subq_6.visits + subq_7.ds__day + , subq_7.metric_time__day + , subq_7.user + , subq_7.visit__referrer_id + , subq_7.visits FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE SELECT - subq_5.ds__day - , subq_5.ds__week - , subq_5.ds__month - , subq_5.ds__quarter - , subq_5.ds__year - , subq_5.ds__extract_year - , subq_5.ds__extract_quarter - , subq_5.ds__extract_month - , subq_5.ds__extract_day - , subq_5.ds__extract_dow - , subq_5.ds__extract_doy - , subq_5.visit__ds__day - , subq_5.visit__ds__week - , subq_5.visit__ds__month - , subq_5.visit__ds__quarter - , subq_5.visit__ds__year - , subq_5.visit__ds__extract_year - , subq_5.visit__ds__extract_quarter - , subq_5.visit__ds__extract_month - , subq_5.visit__ds__extract_day - , subq_5.visit__ds__extract_dow - , subq_5.visit__ds__extract_doy - , subq_5.ds__day AS metric_time__day - , subq_5.ds__week AS metric_time__week - , subq_5.ds__month AS metric_time__month - , subq_5.ds__quarter AS metric_time__quarter - , subq_5.ds__year AS metric_time__year - , subq_5.ds__extract_year AS metric_time__extract_year - , subq_5.ds__extract_quarter AS metric_time__extract_quarter - , subq_5.ds__extract_month AS metric_time__extract_month - , subq_5.ds__extract_day AS metric_time__extract_day - , subq_5.ds__extract_dow AS metric_time__extract_dow - , subq_5.ds__extract_doy AS metric_time__extract_doy - , subq_5.user - , subq_5.session - , subq_5.visit__user - , subq_5.visit__session - , subq_5.referrer_id - , subq_5.visit__referrer_id - , subq_5.visits - , subq_5.visitors + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.metric_time__day + , subq_6.metric_time__week + , subq_6.metric_time__month + , subq_6.metric_time__quarter + , subq_6.metric_time__year + , subq_6.metric_time__extract_year + , subq_6.metric_time__extract_quarter + , subq_6.metric_time__extract_month + , subq_6.metric_time__extract_day + , subq_6.metric_time__extract_dow + , subq_6.metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_5 - ) subq_6 - ) subq_7 + subq_5.ds__day + , subq_5.ds__week + , subq_5.ds__month + , subq_5.ds__quarter + , subq_5.ds__year + , subq_5.ds__extract_year + , subq_5.ds__extract_quarter + , subq_5.ds__extract_month + , subq_5.ds__extract_day + , subq_5.ds__extract_dow + , subq_5.ds__extract_doy + , subq_5.visit__ds__day + , subq_5.visit__ds__week + , subq_5.visit__ds__month + , subq_5.visit__ds__quarter + , subq_5.visit__ds__year + , subq_5.visit__ds__extract_year + , subq_5.visit__ds__extract_quarter + , subq_5.visit__ds__extract_month + , subq_5.visit__ds__extract_day + , subq_5.visit__ds__extract_dow + , subq_5.visit__ds__extract_doy + , subq_5.ds__day AS metric_time__day + , subq_5.ds__week AS metric_time__week + , subq_5.ds__month AS metric_time__month + , subq_5.ds__quarter AS metric_time__quarter + , subq_5.ds__year AS metric_time__year + , subq_5.ds__extract_year AS metric_time__extract_year + , subq_5.ds__extract_quarter AS metric_time__extract_quarter + , subq_5.ds__extract_month AS metric_time__extract_month + , subq_5.ds__extract_day AS metric_time__extract_day + , subq_5.ds__extract_dow AS metric_time__extract_dow + , subq_5.ds__extract_doy AS metric_time__extract_doy + , subq_5.user + , subq_5.session + , subq_5.visit__user + , subq_5.visit__session + , subq_5.referrer_id + , subq_5.visit__referrer_id + , subq_5.visits + , subq_5.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_5 + ) subq_6 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_7 + ) subq_8 INNER JOIN ( -- Add column with generated UUID SELECT - subq_9.ds__day - , subq_9.ds__week - , subq_9.ds__month - , subq_9.ds__quarter - , subq_9.ds__year - , subq_9.ds__extract_year - , subq_9.ds__extract_quarter - , subq_9.ds__extract_month - , subq_9.ds__extract_day - , subq_9.ds__extract_dow - , subq_9.ds__extract_doy - , subq_9.buy__ds__day - , subq_9.buy__ds__week - , subq_9.buy__ds__month - , subq_9.buy__ds__quarter - , subq_9.buy__ds__year - , subq_9.buy__ds__extract_year - , subq_9.buy__ds__extract_quarter - , subq_9.buy__ds__extract_month - , subq_9.buy__ds__extract_day - , subq_9.buy__ds__extract_dow - , subq_9.buy__ds__extract_doy - , subq_9.metric_time__day - , subq_9.metric_time__week - , subq_9.metric_time__month - , subq_9.metric_time__quarter - , subq_9.metric_time__year - , subq_9.metric_time__extract_year - , subq_9.metric_time__extract_quarter - , subq_9.metric_time__extract_month - , subq_9.metric_time__extract_day - , subq_9.metric_time__extract_dow - , subq_9.metric_time__extract_doy - , subq_9.user - , subq_9.session_id - , subq_9.buy__user - , subq_9.buy__session_id - , subq_9.buys - , subq_9.buyers + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.metric_time__day + , subq_10.metric_time__week + , subq_10.metric_time__month + , subq_10.metric_time__quarter + , subq_10.metric_time__year + , subq_10.metric_time__extract_year + , subq_10.metric_time__extract_quarter + , subq_10.metric_time__extract_month + , subq_10.metric_time__extract_day + , subq_10.metric_time__extract_dow + , subq_10.metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers , UUID() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_8.ds__day - , subq_8.ds__week - , subq_8.ds__month - , subq_8.ds__quarter - , subq_8.ds__year - , subq_8.ds__extract_year - , subq_8.ds__extract_quarter - , subq_8.ds__extract_month - , subq_8.ds__extract_day - , subq_8.ds__extract_dow - , subq_8.ds__extract_doy - , subq_8.buy__ds__day - , subq_8.buy__ds__week - , subq_8.buy__ds__month - , subq_8.buy__ds__quarter - , subq_8.buy__ds__year - , subq_8.buy__ds__extract_year - , subq_8.buy__ds__extract_quarter - , subq_8.buy__ds__extract_month - , subq_8.buy__ds__extract_day - , subq_8.buy__ds__extract_dow - , subq_8.buy__ds__extract_doy - , subq_8.ds__day AS metric_time__day - , subq_8.ds__week AS metric_time__week - , subq_8.ds__month AS metric_time__month - , subq_8.ds__quarter AS metric_time__quarter - , subq_8.ds__year AS metric_time__year - , subq_8.ds__extract_year AS metric_time__extract_year - , subq_8.ds__extract_quarter AS metric_time__extract_quarter - , subq_8.ds__extract_month AS metric_time__extract_month - , subq_8.ds__extract_day AS metric_time__extract_day - , subq_8.ds__extract_dow AS metric_time__extract_dow - , subq_8.ds__extract_doy AS metric_time__extract_doy - , subq_8.user - , subq_8.session_id - , subq_8.buy__user - , subq_8.buy__session_id - , subq_8.buys - , subq_8.buyers + subq_9.ds__day + , subq_9.ds__week + , subq_9.ds__month + , subq_9.ds__quarter + , subq_9.ds__year + , subq_9.ds__extract_year + , subq_9.ds__extract_quarter + , subq_9.ds__extract_month + , subq_9.ds__extract_day + , subq_9.ds__extract_dow + , subq_9.ds__extract_doy + , subq_9.buy__ds__day + , subq_9.buy__ds__week + , subq_9.buy__ds__month + , subq_9.buy__ds__quarter + , subq_9.buy__ds__year + , subq_9.buy__ds__extract_year + , subq_9.buy__ds__extract_quarter + , subq_9.buy__ds__extract_month + , subq_9.buy__ds__extract_day + , subq_9.buy__ds__extract_dow + , subq_9.buy__ds__extract_doy + , subq_9.ds__day AS metric_time__day + , subq_9.ds__week AS metric_time__week + , subq_9.ds__month AS metric_time__month + , subq_9.ds__quarter AS metric_time__quarter + , subq_9.ds__year AS metric_time__year + , subq_9.ds__extract_year AS metric_time__extract_year + , subq_9.ds__extract_quarter AS metric_time__extract_quarter + , subq_9.ds__extract_month AS metric_time__extract_month + , subq_9.ds__extract_day AS metric_time__extract_day + , subq_9.ds__extract_dow AS metric_time__extract_dow + , subq_9.ds__extract_doy AS metric_time__extract_doy + , subq_9.user + , subq_9.session_id + , subq_9.buy__user + , subq_9.buy__session_id + , subq_9.buys + , subq_9.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -387,29 +433,29 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_8 - ) subq_9 - ) subq_10 + ) subq_9 + ) subq_10 + ) subq_11 ON ( - subq_7.user = subq_10.user + subq_8.user = subq_11.user ) AND ( - (subq_7.ds__day <= subq_10.ds__day) + (subq_8.ds__day <= subq_11.ds__day) ) - ) subq_11 - ) subq_12 - ) subq_13 + ) subq_12 + ) subq_13 + ) subq_14 GROUP BY - subq_13.metric_time__day - , subq_13.visit__referrer_id - ) subq_14 + subq_14.metric_time__day + , subq_14.visit__referrer_id + ) subq_15 ON ( - subq_4.visit__referrer_id = subq_14.visit__referrer_id + subq_4.visit__referrer_id = subq_15.visit__referrer_id ) AND ( - subq_4.metric_time__day = subq_14.metric_time__day + subq_4.metric_time__day = subq_15.metric_time__day ) GROUP BY - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) - , COALESCE(subq_4.visit__referrer_id, subq_14.visit__referrer_id) -) subq_15 + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) + , COALESCE(subq_4.visit__referrer_id, subq_15.visit__referrer_id) +) subq_16 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_categorical_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_categorical_filter__plan0_optimized.sql index 2d192fb79d..c94d8ec89f 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_categorical_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_categorical_filter__plan0_optimized.sql @@ -6,10 +6,10 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , COALESCE(subq_20.visit__referrer_id, subq_30.visit__referrer_id) AS visit__referrer_id - , MAX(subq_20.visits) AS visits - , MAX(subq_30.buys) AS buys + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , COALESCE(subq_21.visit__referrer_id, subq_32.visit__referrer_id) AS visit__referrer_id + , MAX(subq_21.visits) AS visits + , MAX(subq_32.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -26,12 +26,12 @@ FROM ( , referrer_id AS visit__referrer_id , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_18 + ) subq_19 WHERE visit__referrer_id = 'ref_id_01' GROUP BY metric_time__day , visit__referrer_id - ) subq_20 + ) subq_21 FULL OUTER JOIN ( -- Find conversions for user within the range of INF -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] @@ -43,60 +43,70 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_23.visits) OVER ( + FIRST_VALUE(subq_25.visits) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_23.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_25.visit__referrer_id) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_23.ds__day) OVER ( + , FIRST_VALUE(subq_25.ds__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_23.metric_time__day) OVER ( + , FIRST_VALUE(subq_25.metric_time__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_23.user) OVER ( + , FIRST_VALUE(subq_25.user) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_26.mf_internal_uuid AS mf_internal_uuid - , subq_26.buys AS buys + , subq_28.mf_internal_uuid AS mf_internal_uuid + , subq_28.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + ds__day + , metric_time__day + , subq_23.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_23 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_25 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -107,25 +117,25 @@ FROM ( , 1 AS buys , UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_26 + ) subq_28 ON ( - subq_23.user = subq_26.user + subq_25.user = subq_28.user ) AND ( - (subq_23.ds__day <= subq_26.ds__day) + (subq_25.ds__day <= subq_28.ds__day) ) - ) subq_27 + ) subq_29 GROUP BY metric_time__day , visit__referrer_id - ) subq_30 + ) subq_32 ON ( - subq_20.visit__referrer_id = subq_30.visit__referrer_id + subq_21.visit__referrer_id = subq_32.visit__referrer_id ) AND ( - subq_20.metric_time__day = subq_30.metric_time__day + subq_21.metric_time__day = subq_32.metric_time__day ) GROUP BY - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) - , COALESCE(subq_20.visit__referrer_id, subq_30.visit__referrer_id) -) subq_31 + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) + , COALESCE(subq_21.visit__referrer_id, subq_32.visit__referrer_id) +) subq_33 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_filter__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_filter__plan0.sql new file mode 100644 index 0000000000..0c95bb623c --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_filter__plan0.sql @@ -0,0 +1,429 @@ +-- Compute Metrics via Expressions +SELECT + CAST(subq_17.buys AS DOUBLE) / CAST(NULLIF(subq_17.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_5.visits) AS visits + , MAX(subq_16.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_4.visits) AS visits + FROM ( + -- Pass Only Elements: ['visits',] + SELECT + subq_3.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_2.metric_time__day + , subq_2.visits + FROM ( + -- Pass Only Elements: ['visits', 'metric_time__day'] + SELECT + subq_1.metric_time__day + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_0 + ) subq_1 + ) subq_2 + WHERE metric_time__day = '2020-01-01' + ) subq_3 + ) subq_4 + ) subq_5 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_15.buys) AS buys + FROM ( + -- Pass Only Elements: ['buys',] + SELECT + subq_14.buys + FROM ( + -- Find conversions for user within the range of INF + SELECT + subq_13.ds__day + , subq_13.metric_time__day + , subq_13.user + , subq_13.buys + , subq_13.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_9.visits) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_9.ds__day) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_9.metric_time__day) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS metric_time__day + , FIRST_VALUE(subq_9.user) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_12.mf_internal_uuid AS mf_internal_uuid + , subq_12.buys AS buys + FROM ( + -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] + SELECT + subq_8.ds__day + , subq_8.metric_time__day + , subq_8.user + , subq_8.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE metric_time__day = '2020-01-01' + ) subq_8 + ) subq_9 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.metric_time__day + , subq_11.metric_time__week + , subq_11.metric_time__month + , subq_11.metric_time__quarter + , subq_11.metric_time__year + , subq_11.metric_time__extract_year + , subq_11.metric_time__extract_quarter + , subq_11.metric_time__extract_month + , subq_11.metric_time__extract_day + , subq_11.metric_time__extract_dow + , subq_11.metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers + , UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.ds__day AS metric_time__day + , subq_10.ds__week AS metric_time__week + , subq_10.ds__month AS metric_time__month + , subq_10.ds__quarter AS metric_time__quarter + , subq_10.ds__year AS metric_time__year + , subq_10.ds__extract_year AS metric_time__extract_year + , subq_10.ds__extract_quarter AS metric_time__extract_quarter + , subq_10.ds__extract_month AS metric_time__extract_month + , subq_10.ds__extract_day AS metric_time__extract_day + , subq_10.ds__extract_dow AS metric_time__extract_dow + , subq_10.ds__extract_doy AS metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_28000.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM buys_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_28000.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM buys_source_src_28000.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS buy__ds__extract_doy + , buys_source_src_28000.user_id AS user + , buys_source_src_28000.session_id + , buys_source_src_28000.user_id AS buy__user + , buys_source_src_28000.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_10 + ) subq_11 + ) subq_12 + ON + ( + subq_9.user = subq_12.user + ) AND ( + (subq_9.ds__day <= subq_12.ds__day) + ) + ) subq_13 + ) subq_14 + ) subq_15 + ) subq_16 +) subq_17 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_filter__plan0_optimized.sql new file mode 100644 index 0000000000..be90af1125 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_filter__plan0_optimized.sql @@ -0,0 +1,103 @@ +-- Combine Aggregated Outputs +-- Compute Metrics via Expressions +SELECT + CAST(MAX(subq_34.buys) AS DOUBLE) / CAST(NULLIF(MAX(subq_23.visits), 0) AS DOUBLE) AS visit_buy_conversion_rate +FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits',] + -- Aggregate Measures + SELECT + 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_20 + WHERE metric_time__day = '2020-01-01' +) subq_23 +CROSS JOIN ( + -- Find conversions for user within the range of INF + -- Pass Only Elements: ['buys',] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_27.visits) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_27.ds__day) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_27.metric_time__day) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS metric_time__day + , FIRST_VALUE(subq_27.user) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_30.mf_internal_uuid AS mf_internal_uuid + , subq_30.buys AS buys + FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] + SELECT + ds__day + , metric_time__day + , subq_25.user + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_25 + WHERE metric_time__day = '2020-01-01' + ) subq_27 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_30 + ON + ( + subq_27.user = subq_30.user + ) AND ( + (subq_27.ds__day <= subq_30.ds__day) + ) + ) subq_31 +) subq_34 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_filter_not_in_group_by__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_filter_not_in_group_by__plan0.sql new file mode 100644 index 0000000000..b918b77a7f --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_filter_not_in_group_by__plan0.sql @@ -0,0 +1,433 @@ +-- Compute Metrics via Expressions +SELECT + subq_17.buys AS visit_buy_conversions +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_5.visits) AS visits + , COALESCE(MAX(subq_16.buys), 0) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_4.visits) AS visits + FROM ( + -- Pass Only Elements: ['visits',] + SELECT + subq_3.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_2.visit__referrer_id + , subq_2.visits + FROM ( + -- Pass Only Elements: ['visits', 'visit__referrer_id'] + SELECT + subq_1.visit__referrer_id + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_0 + ) subq_1 + ) subq_2 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_3 + ) subq_4 + ) subq_5 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_15.buys) AS buys + FROM ( + -- Pass Only Elements: ['buys',] + SELECT + subq_14.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_13.ds__day + , subq_13.user + , subq_13.visit__referrer_id + , subq_13.buys + , subq_13.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_9.visits) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_9.visit__referrer_id) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visit__referrer_id + , FIRST_VALUE(subq_9.ds__day) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_9.user) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_12.mf_internal_uuid AS mf_internal_uuid + , subq_12.buys AS buys + FROM ( + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] + SELECT + subq_8.ds__day + , subq_8.user + , subq_8.visit__referrer_id + , subq_8.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_8 + ) subq_9 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.metric_time__day + , subq_11.metric_time__week + , subq_11.metric_time__month + , subq_11.metric_time__quarter + , subq_11.metric_time__year + , subq_11.metric_time__extract_year + , subq_11.metric_time__extract_quarter + , subq_11.metric_time__extract_month + , subq_11.metric_time__extract_day + , subq_11.metric_time__extract_dow + , subq_11.metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers + , UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.ds__day AS metric_time__day + , subq_10.ds__week AS metric_time__week + , subq_10.ds__month AS metric_time__month + , subq_10.ds__quarter AS metric_time__quarter + , subq_10.ds__year AS metric_time__year + , subq_10.ds__extract_year AS metric_time__extract_year + , subq_10.ds__extract_quarter AS metric_time__extract_quarter + , subq_10.ds__extract_month AS metric_time__extract_month + , subq_10.ds__extract_day AS metric_time__extract_day + , subq_10.ds__extract_dow AS metric_time__extract_dow + , subq_10.ds__extract_doy AS metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_28000.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM buys_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_28000.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM buys_source_src_28000.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS buy__ds__extract_doy + , buys_source_src_28000.user_id AS user + , buys_source_src_28000.session_id + , buys_source_src_28000.user_id AS buy__user + , buys_source_src_28000.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_10 + ) subq_11 + ) subq_12 + ON + ( + subq_9.user = subq_12.user + ) AND ( + ( + subq_9.ds__day <= subq_12.ds__day + ) AND ( + subq_9.ds__day > DATEADD(day, -7, subq_12.ds__day) + ) + ) + ) subq_13 + ) subq_14 + ) subq_15 + ) subq_16 +) subq_17 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_filter_not_in_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_filter_not_in_group_by__plan0_optimized.sql new file mode 100644 index 0000000000..050dc71c60 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_filter_not_in_group_by__plan0_optimized.sql @@ -0,0 +1,107 @@ +-- Combine Aggregated Outputs +-- Compute Metrics via Expressions +SELECT + COALESCE(MAX(subq_34.buys), 0) AS visit_buy_conversions +FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits',] + -- Aggregate Measures + SELECT + 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_20 + WHERE visit__referrer_id = 'ref_id_01' +) subq_23 +CROSS JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: ['buys',] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_27.visits) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_27.visit__referrer_id) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visit__referrer_id + , FIRST_VALUE(subq_27.ds__day) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_27.user) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_30.mf_internal_uuid AS mf_internal_uuid + , subq_30.buys AS buys + FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] + SELECT + ds__day + , subq_25.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_25 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_27 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_30 + ON + ( + subq_27.user = subq_30.user + ) AND ( + ( + subq_27.ds__day <= subq_30.ds__day + ) AND ( + subq_27.ds__day > DATEADD(day, -7, subq_30.ds__day) + ) + ) + ) subq_31 +) subq_34 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_time_constraint__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_time_constraint__plan0.sql index 2d5dbf536a..f49594dfd6 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_time_constraint__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_time_constraint__plan0.sql @@ -1,13 +1,13 @@ -- Compute Metrics via Expressions SELECT - subq_17.visit__referrer_id - , CAST(subq_17.buys AS DOUBLE) / CAST(NULLIF(subq_17.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate + subq_18.visit__referrer_id + , CAST(subq_18.buys AS DOUBLE) / CAST(NULLIF(subq_18.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) AS visit__referrer_id , MAX(subq_5.visits) AS visits - , MAX(subq_16.buys) AS buys + , MAX(subq_17.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -158,277 +158,323 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_15.visit__referrer_id - , SUM(subq_15.buys) AS buys + subq_16.visit__referrer_id + , SUM(subq_16.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'visit__referrer_id'] SELECT - subq_14.visit__referrer_id - , subq_14.buys + subq_15.visit__referrer_id + , subq_15.buys FROM ( -- Find conversions for user within the range of INF SELECT - subq_13.ds__day - , subq_13.user - , subq_13.visit__referrer_id - , subq_13.buys - , subq_13.visits + subq_14.ds__day + , subq_14.user + , subq_14.visit__referrer_id + , subq_14.buys + , subq_14.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_9.visits) OVER ( + FIRST_VALUE(subq_10.visits) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_9.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_10.visit__referrer_id) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_9.ds__day) OVER ( + , FIRST_VALUE(subq_10.ds__day) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_9.user) OVER ( + , FIRST_VALUE(subq_10.user) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_12.mf_internal_uuid AS mf_internal_uuid - , subq_12.buys AS buys + , subq_13.mf_internal_uuid AS mf_internal_uuid + , subq_13.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] SELECT - subq_8.ds__day - , subq_8.user - , subq_8.visit__referrer_id - , subq_8.visits + subq_9.ds__day + , subq_9.user + , subq_9.visit__referrer_id + , subq_9.visits FROM ( - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE SELECT - subq_7.ds__day - , subq_7.ds__week - , subq_7.ds__month - , subq_7.ds__quarter - , subq_7.ds__year - , subq_7.ds__extract_year - , subq_7.ds__extract_quarter - , subq_7.ds__extract_month - , subq_7.ds__extract_day - , subq_7.ds__extract_dow - , subq_7.ds__extract_doy - , subq_7.visit__ds__day - , subq_7.visit__ds__week - , subq_7.visit__ds__month - , subq_7.visit__ds__quarter - , subq_7.visit__ds__year - , subq_7.visit__ds__extract_year - , subq_7.visit__ds__extract_quarter - , subq_7.visit__ds__extract_month - , subq_7.visit__ds__extract_day - , subq_7.visit__ds__extract_dow - , subq_7.visit__ds__extract_doy - , subq_7.metric_time__day - , subq_7.metric_time__week - , subq_7.metric_time__month - , subq_7.metric_time__quarter - , subq_7.metric_time__year - , subq_7.metric_time__extract_year - , subq_7.metric_time__extract_quarter - , subq_7.metric_time__extract_month - , subq_7.metric_time__extract_day - , subq_7.metric_time__extract_dow - , subq_7.metric_time__extract_doy - , subq_7.user - , subq_7.session - , subq_7.visit__user - , subq_7.visit__session - , subq_7.referrer_id - , subq_7.visit__referrer_id - , subq_7.visits - , subq_7.visitors + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.visit__ds__day + , subq_8.visit__ds__week + , subq_8.visit__ds__month + , subq_8.visit__ds__quarter + , subq_8.visit__ds__year + , subq_8.visit__ds__extract_year + , subq_8.visit__ds__extract_quarter + , subq_8.visit__ds__extract_month + , subq_8.visit__ds__extract_day + , subq_8.visit__ds__extract_dow + , subq_8.visit__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session + , subq_8.visit__user + , subq_8.visit__session + , subq_8.referrer_id + , subq_8.visit__referrer_id + , subq_8.visits + , subq_8.visitors FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] SELECT - subq_6.ds__day - , subq_6.ds__week - , subq_6.ds__month - , subq_6.ds__quarter - , subq_6.ds__year - , subq_6.ds__extract_year - , subq_6.ds__extract_quarter - , subq_6.ds__extract_month - , subq_6.ds__extract_day - , subq_6.ds__extract_dow - , subq_6.ds__extract_doy - , subq_6.visit__ds__day - , subq_6.visit__ds__week - , subq_6.visit__ds__month - , subq_6.visit__ds__quarter - , subq_6.visit__ds__year - , subq_6.visit__ds__extract_year - , subq_6.visit__ds__extract_quarter - , subq_6.visit__ds__extract_month - , subq_6.visit__ds__extract_day - , subq_6.visit__ds__extract_dow - , subq_6.visit__ds__extract_doy - , subq_6.ds__day AS metric_time__day - , subq_6.ds__week AS metric_time__week - , subq_6.ds__month AS metric_time__month - , subq_6.ds__quarter AS metric_time__quarter - , subq_6.ds__year AS metric_time__year - , subq_6.ds__extract_year AS metric_time__extract_year - , subq_6.ds__extract_quarter AS metric_time__extract_quarter - , subq_6.ds__extract_month AS metric_time__extract_month - , subq_6.ds__extract_day AS metric_time__extract_day - , subq_6.ds__extract_dow AS metric_time__extract_dow - , subq_6.ds__extract_doy AS metric_time__extract_doy - , subq_6.user - , subq_6.session - , subq_6.visit__user - , subq_6.visit__session - , subq_6.referrer_id - , subq_6.visit__referrer_id - , subq_6.visits - , subq_6.visitors + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_6 - ) subq_7 - WHERE subq_7.metric_time__day BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_8 - ) subq_9 + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE subq_7.metric_time__day BETWEEN '2020-01-01' AND '2020-01-02' + ) subq_8 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_9 + ) subq_10 INNER JOIN ( -- Add column with generated UUID SELECT - subq_11.ds__day - , subq_11.ds__week - , subq_11.ds__month - , subq_11.ds__quarter - , subq_11.ds__year - , subq_11.ds__extract_year - , subq_11.ds__extract_quarter - , subq_11.ds__extract_month - , subq_11.ds__extract_day - , subq_11.ds__extract_dow - , subq_11.ds__extract_doy - , subq_11.buy__ds__day - , subq_11.buy__ds__week - , subq_11.buy__ds__month - , subq_11.buy__ds__quarter - , subq_11.buy__ds__year - , subq_11.buy__ds__extract_year - , subq_11.buy__ds__extract_quarter - , subq_11.buy__ds__extract_month - , subq_11.buy__ds__extract_day - , subq_11.buy__ds__extract_dow - , subq_11.buy__ds__extract_doy - , subq_11.metric_time__day - , subq_11.metric_time__week - , subq_11.metric_time__month - , subq_11.metric_time__quarter - , subq_11.metric_time__year - , subq_11.metric_time__extract_year - , subq_11.metric_time__extract_quarter - , subq_11.metric_time__extract_month - , subq_11.metric_time__extract_day - , subq_11.metric_time__extract_dow - , subq_11.metric_time__extract_doy - , subq_11.user - , subq_11.session_id - , subq_11.buy__user - , subq_11.buy__session_id - , subq_11.buys - , subq_11.buyers + subq_12.ds__day + , subq_12.ds__week + , subq_12.ds__month + , subq_12.ds__quarter + , subq_12.ds__year + , subq_12.ds__extract_year + , subq_12.ds__extract_quarter + , subq_12.ds__extract_month + , subq_12.ds__extract_day + , subq_12.ds__extract_dow + , subq_12.ds__extract_doy + , subq_12.buy__ds__day + , subq_12.buy__ds__week + , subq_12.buy__ds__month + , subq_12.buy__ds__quarter + , subq_12.buy__ds__year + , subq_12.buy__ds__extract_year + , subq_12.buy__ds__extract_quarter + , subq_12.buy__ds__extract_month + , subq_12.buy__ds__extract_day + , subq_12.buy__ds__extract_dow + , subq_12.buy__ds__extract_doy + , subq_12.metric_time__day + , subq_12.metric_time__week + , subq_12.metric_time__month + , subq_12.metric_time__quarter + , subq_12.metric_time__year + , subq_12.metric_time__extract_year + , subq_12.metric_time__extract_quarter + , subq_12.metric_time__extract_month + , subq_12.metric_time__extract_day + , subq_12.metric_time__extract_dow + , subq_12.metric_time__extract_doy + , subq_12.user + , subq_12.session_id + , subq_12.buy__user + , subq_12.buy__session_id + , subq_12.buys + , subq_12.buyers , UUID() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_10.ds__day - , subq_10.ds__week - , subq_10.ds__month - , subq_10.ds__quarter - , subq_10.ds__year - , subq_10.ds__extract_year - , subq_10.ds__extract_quarter - , subq_10.ds__extract_month - , subq_10.ds__extract_day - , subq_10.ds__extract_dow - , subq_10.ds__extract_doy - , subq_10.buy__ds__day - , subq_10.buy__ds__week - , subq_10.buy__ds__month - , subq_10.buy__ds__quarter - , subq_10.buy__ds__year - , subq_10.buy__ds__extract_year - , subq_10.buy__ds__extract_quarter - , subq_10.buy__ds__extract_month - , subq_10.buy__ds__extract_day - , subq_10.buy__ds__extract_dow - , subq_10.buy__ds__extract_doy - , subq_10.ds__day AS metric_time__day - , subq_10.ds__week AS metric_time__week - , subq_10.ds__month AS metric_time__month - , subq_10.ds__quarter AS metric_time__quarter - , subq_10.ds__year AS metric_time__year - , subq_10.ds__extract_year AS metric_time__extract_year - , subq_10.ds__extract_quarter AS metric_time__extract_quarter - , subq_10.ds__extract_month AS metric_time__extract_month - , subq_10.ds__extract_day AS metric_time__extract_day - , subq_10.ds__extract_dow AS metric_time__extract_dow - , subq_10.ds__extract_doy AS metric_time__extract_doy - , subq_10.user - , subq_10.session_id - , subq_10.buy__user - , subq_10.buy__session_id - , subq_10.buys - , subq_10.buyers + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.ds__day AS metric_time__day + , subq_11.ds__week AS metric_time__week + , subq_11.ds__month AS metric_time__month + , subq_11.ds__quarter AS metric_time__quarter + , subq_11.ds__year AS metric_time__year + , subq_11.ds__extract_year AS metric_time__extract_year + , subq_11.ds__extract_quarter AS metric_time__extract_quarter + , subq_11.ds__extract_month AS metric_time__extract_month + , subq_11.ds__extract_day AS metric_time__extract_day + , subq_11.ds__extract_dow AS metric_time__extract_dow + , subq_11.ds__extract_doy AS metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -461,23 +507,23 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_10 - ) subq_11 - ) subq_12 + ) subq_11 + ) subq_12 + ) subq_13 ON ( - subq_9.user = subq_12.user + subq_10.user = subq_13.user ) AND ( - (subq_9.ds__day <= subq_12.ds__day) + (subq_10.ds__day <= subq_13.ds__day) ) - ) subq_13 - ) subq_14 - ) subq_15 + ) subq_14 + ) subq_15 + ) subq_16 GROUP BY - subq_15.visit__referrer_id - ) subq_16 + subq_16.visit__referrer_id + ) subq_17 ON - subq_5.visit__referrer_id = subq_16.visit__referrer_id + subq_5.visit__referrer_id = subq_17.visit__referrer_id GROUP BY - COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) -) subq_17 + COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) +) subq_18 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_time_constraint__plan0_optimized.sql index 0df77ffb03..f93f3d5c30 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_time_constraint__plan0_optimized.sql @@ -5,9 +5,9 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) AS visit__referrer_id - , MAX(subq_23.visits) AS visits - , MAX(subq_34.buys) AS buys + COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) AS visit__referrer_id + , MAX(subq_24.visits) AS visits + , MAX(subq_36.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -24,11 +24,11 @@ FROM ( , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_21 + ) subq_22 WHERE visit__referrer_id = 'ref_id_01' GROUP BY visit__referrer_id - ) subq_23 + ) subq_24 FULL OUTER JOIN ( -- Find conversions for user within the range of INF -- Pass Only Elements: ['buys', 'visit__referrer_id'] @@ -39,53 +39,62 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_27.visits) OVER ( + FIRST_VALUE(subq_29.visits) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_27.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_29.visit__referrer_id) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_27.ds__day) OVER ( + , FIRST_VALUE(subq_29.ds__day) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_27.user) OVER ( + , FIRST_VALUE(subq_29.user) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_30.mf_internal_uuid AS mf_internal_uuid - , subq_30.buys AS buys + , subq_32.mf_internal_uuid AS mf_internal_uuid + , subq_32.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_27 + ds__day + , subq_27.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' + ) subq_27 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_29 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -96,19 +105,19 @@ FROM ( , 1 AS buys , UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_30 + ) subq_32 ON ( - subq_27.user = subq_30.user + subq_29.user = subq_32.user ) AND ( - (subq_27.ds__day <= subq_30.ds__day) + (subq_29.ds__day <= subq_32.ds__day) ) - ) subq_31 + ) subq_33 GROUP BY visit__referrer_id - ) subq_34 + ) subq_36 ON - subq_23.visit__referrer_id = subq_34.visit__referrer_id + subq_24.visit__referrer_id = subq_36.visit__referrer_id GROUP BY - COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) -) subq_35 + COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) +) subq_37 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_window__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_window__plan0.sql index 6948316384..dc477c42ce 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_window__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_window__plan0.sql @@ -1,13 +1,13 @@ -- Compute Metrics via Expressions SELECT - subq_15.metric_time__day - , CAST(subq_15.buys AS DOUBLE) / CAST(NULLIF(subq_15.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days + subq_16.metric_time__day + , CAST(subq_16.buys AS DOUBLE) / CAST(NULLIF(subq_16.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) AS metric_time__day + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) AS metric_time__day , MAX(subq_4.visits) AS visits - , MAX(subq_14.buys) AS buys + , MAX(subq_15.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -112,231 +112,277 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_13.metric_time__day - , SUM(subq_13.buys) AS buys + subq_14.metric_time__day + , SUM(subq_14.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'metric_time__day'] SELECT - subq_12.metric_time__day - , subq_12.buys + subq_13.metric_time__day + , subq_13.buys FROM ( -- Find conversions for user within the range of 7 day SELECT - subq_11.ds__day - , subq_11.metric_time__day - , subq_11.user - , subq_11.buys - , subq_11.visits + subq_12.ds__day + , subq_12.metric_time__day + , subq_12.user + , subq_12.buys + , subq_12.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_7.visits) OVER ( + FIRST_VALUE(subq_8.visits) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_7.ds__day) OVER ( + , FIRST_VALUE(subq_8.ds__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_7.metric_time__day) OVER ( + , FIRST_VALUE(subq_8.metric_time__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_7.user) OVER ( + , FIRST_VALUE(subq_8.user) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_10.mf_internal_uuid AS mf_internal_uuid - , subq_10.buys AS buys + , subq_11.mf_internal_uuid AS mf_internal_uuid + , subq_11.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_6.ds__day - , subq_6.metric_time__day - , subq_6.user - , subq_6.visits + subq_7.ds__day + , subq_7.metric_time__day + , subq_7.user + , subq_7.visits FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE SELECT - subq_5.ds__day - , subq_5.ds__week - , subq_5.ds__month - , subq_5.ds__quarter - , subq_5.ds__year - , subq_5.ds__extract_year - , subq_5.ds__extract_quarter - , subq_5.ds__extract_month - , subq_5.ds__extract_day - , subq_5.ds__extract_dow - , subq_5.ds__extract_doy - , subq_5.visit__ds__day - , subq_5.visit__ds__week - , subq_5.visit__ds__month - , subq_5.visit__ds__quarter - , subq_5.visit__ds__year - , subq_5.visit__ds__extract_year - , subq_5.visit__ds__extract_quarter - , subq_5.visit__ds__extract_month - , subq_5.visit__ds__extract_day - , subq_5.visit__ds__extract_dow - , subq_5.visit__ds__extract_doy - , subq_5.ds__day AS metric_time__day - , subq_5.ds__week AS metric_time__week - , subq_5.ds__month AS metric_time__month - , subq_5.ds__quarter AS metric_time__quarter - , subq_5.ds__year AS metric_time__year - , subq_5.ds__extract_year AS metric_time__extract_year - , subq_5.ds__extract_quarter AS metric_time__extract_quarter - , subq_5.ds__extract_month AS metric_time__extract_month - , subq_5.ds__extract_day AS metric_time__extract_day - , subq_5.ds__extract_dow AS metric_time__extract_dow - , subq_5.ds__extract_doy AS metric_time__extract_doy - , subq_5.user - , subq_5.session - , subq_5.visit__user - , subq_5.visit__session - , subq_5.referrer_id - , subq_5.visit__referrer_id - , subq_5.visits - , subq_5.visitors + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.metric_time__day + , subq_6.metric_time__week + , subq_6.metric_time__month + , subq_6.metric_time__quarter + , subq_6.metric_time__year + , subq_6.metric_time__extract_year + , subq_6.metric_time__extract_quarter + , subq_6.metric_time__extract_month + , subq_6.metric_time__extract_day + , subq_6.metric_time__extract_dow + , subq_6.metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_5 - ) subq_6 - ) subq_7 + subq_5.ds__day + , subq_5.ds__week + , subq_5.ds__month + , subq_5.ds__quarter + , subq_5.ds__year + , subq_5.ds__extract_year + , subq_5.ds__extract_quarter + , subq_5.ds__extract_month + , subq_5.ds__extract_day + , subq_5.ds__extract_dow + , subq_5.ds__extract_doy + , subq_5.visit__ds__day + , subq_5.visit__ds__week + , subq_5.visit__ds__month + , subq_5.visit__ds__quarter + , subq_5.visit__ds__year + , subq_5.visit__ds__extract_year + , subq_5.visit__ds__extract_quarter + , subq_5.visit__ds__extract_month + , subq_5.visit__ds__extract_day + , subq_5.visit__ds__extract_dow + , subq_5.visit__ds__extract_doy + , subq_5.ds__day AS metric_time__day + , subq_5.ds__week AS metric_time__week + , subq_5.ds__month AS metric_time__month + , subq_5.ds__quarter AS metric_time__quarter + , subq_5.ds__year AS metric_time__year + , subq_5.ds__extract_year AS metric_time__extract_year + , subq_5.ds__extract_quarter AS metric_time__extract_quarter + , subq_5.ds__extract_month AS metric_time__extract_month + , subq_5.ds__extract_day AS metric_time__extract_day + , subq_5.ds__extract_dow AS metric_time__extract_dow + , subq_5.ds__extract_doy AS metric_time__extract_doy + , subq_5.user + , subq_5.session + , subq_5.visit__user + , subq_5.visit__session + , subq_5.referrer_id + , subq_5.visit__referrer_id + , subq_5.visits + , subq_5.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_5 + ) subq_6 + WHERE metric_time__day = '2020-01-01' + ) subq_7 + ) subq_8 INNER JOIN ( -- Add column with generated UUID SELECT - subq_9.ds__day - , subq_9.ds__week - , subq_9.ds__month - , subq_9.ds__quarter - , subq_9.ds__year - , subq_9.ds__extract_year - , subq_9.ds__extract_quarter - , subq_9.ds__extract_month - , subq_9.ds__extract_day - , subq_9.ds__extract_dow - , subq_9.ds__extract_doy - , subq_9.buy__ds__day - , subq_9.buy__ds__week - , subq_9.buy__ds__month - , subq_9.buy__ds__quarter - , subq_9.buy__ds__year - , subq_9.buy__ds__extract_year - , subq_9.buy__ds__extract_quarter - , subq_9.buy__ds__extract_month - , subq_9.buy__ds__extract_day - , subq_9.buy__ds__extract_dow - , subq_9.buy__ds__extract_doy - , subq_9.metric_time__day - , subq_9.metric_time__week - , subq_9.metric_time__month - , subq_9.metric_time__quarter - , subq_9.metric_time__year - , subq_9.metric_time__extract_year - , subq_9.metric_time__extract_quarter - , subq_9.metric_time__extract_month - , subq_9.metric_time__extract_day - , subq_9.metric_time__extract_dow - , subq_9.metric_time__extract_doy - , subq_9.user - , subq_9.session_id - , subq_9.buy__user - , subq_9.buy__session_id - , subq_9.buys - , subq_9.buyers + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.metric_time__day + , subq_10.metric_time__week + , subq_10.metric_time__month + , subq_10.metric_time__quarter + , subq_10.metric_time__year + , subq_10.metric_time__extract_year + , subq_10.metric_time__extract_quarter + , subq_10.metric_time__extract_month + , subq_10.metric_time__extract_day + , subq_10.metric_time__extract_dow + , subq_10.metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers , UUID() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_8.ds__day - , subq_8.ds__week - , subq_8.ds__month - , subq_8.ds__quarter - , subq_8.ds__year - , subq_8.ds__extract_year - , subq_8.ds__extract_quarter - , subq_8.ds__extract_month - , subq_8.ds__extract_day - , subq_8.ds__extract_dow - , subq_8.ds__extract_doy - , subq_8.buy__ds__day - , subq_8.buy__ds__week - , subq_8.buy__ds__month - , subq_8.buy__ds__quarter - , subq_8.buy__ds__year - , subq_8.buy__ds__extract_year - , subq_8.buy__ds__extract_quarter - , subq_8.buy__ds__extract_month - , subq_8.buy__ds__extract_day - , subq_8.buy__ds__extract_dow - , subq_8.buy__ds__extract_doy - , subq_8.ds__day AS metric_time__day - , subq_8.ds__week AS metric_time__week - , subq_8.ds__month AS metric_time__month - , subq_8.ds__quarter AS metric_time__quarter - , subq_8.ds__year AS metric_time__year - , subq_8.ds__extract_year AS metric_time__extract_year - , subq_8.ds__extract_quarter AS metric_time__extract_quarter - , subq_8.ds__extract_month AS metric_time__extract_month - , subq_8.ds__extract_day AS metric_time__extract_day - , subq_8.ds__extract_dow AS metric_time__extract_dow - , subq_8.ds__extract_doy AS metric_time__extract_doy - , subq_8.user - , subq_8.session_id - , subq_8.buy__user - , subq_8.buy__session_id - , subq_8.buys - , subq_8.buyers + subq_9.ds__day + , subq_9.ds__week + , subq_9.ds__month + , subq_9.ds__quarter + , subq_9.ds__year + , subq_9.ds__extract_year + , subq_9.ds__extract_quarter + , subq_9.ds__extract_month + , subq_9.ds__extract_day + , subq_9.ds__extract_dow + , subq_9.ds__extract_doy + , subq_9.buy__ds__day + , subq_9.buy__ds__week + , subq_9.buy__ds__month + , subq_9.buy__ds__quarter + , subq_9.buy__ds__year + , subq_9.buy__ds__extract_year + , subq_9.buy__ds__extract_quarter + , subq_9.buy__ds__extract_month + , subq_9.buy__ds__extract_day + , subq_9.buy__ds__extract_dow + , subq_9.buy__ds__extract_doy + , subq_9.ds__day AS metric_time__day + , subq_9.ds__week AS metric_time__week + , subq_9.ds__month AS metric_time__month + , subq_9.ds__quarter AS metric_time__quarter + , subq_9.ds__year AS metric_time__year + , subq_9.ds__extract_year AS metric_time__extract_year + , subq_9.ds__extract_quarter AS metric_time__extract_quarter + , subq_9.ds__extract_month AS metric_time__extract_month + , subq_9.ds__extract_day AS metric_time__extract_day + , subq_9.ds__extract_dow AS metric_time__extract_dow + , subq_9.ds__extract_doy AS metric_time__extract_doy + , subq_9.user + , subq_9.session_id + , subq_9.buy__user + , subq_9.buy__session_id + , subq_9.buys + , subq_9.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -369,27 +415,27 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_8 - ) subq_9 - ) subq_10 + ) subq_9 + ) subq_10 + ) subq_11 ON ( - subq_7.user = subq_10.user + subq_8.user = subq_11.user ) AND ( ( - subq_7.ds__day <= subq_10.ds__day + subq_8.ds__day <= subq_11.ds__day ) AND ( - subq_7.ds__day > DATEADD(day, -7, subq_10.ds__day) + subq_8.ds__day > DATEADD(day, -7, subq_11.ds__day) ) ) - ) subq_11 - ) subq_12 - ) subq_13 + ) subq_12 + ) subq_13 + ) subq_14 GROUP BY - subq_13.metric_time__day - ) subq_14 + subq_14.metric_time__day + ) subq_15 ON - subq_4.metric_time__day = subq_14.metric_time__day + subq_4.metric_time__day = subq_15.metric_time__day GROUP BY - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) -) subq_15 + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) +) subq_16 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_window__plan0_optimized.sql index 2a6d80d278..44656bd627 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_window__plan0_optimized.sql @@ -5,9 +5,9 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_20.visits) AS visits - , MAX(subq_30.buys) AS buys + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , MAX(subq_21.visits) AS visits + , MAX(subq_32.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -22,11 +22,11 @@ FROM ( DATE_TRUNC('day', ds) AS metric_time__day , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_18 + ) subq_19 WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__day - ) subq_20 + ) subq_21 FULL OUTER JOIN ( -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'metric_time__day'] @@ -37,51 +37,60 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_23.visits) OVER ( + FIRST_VALUE(subq_25.visits) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_23.ds__day) OVER ( + , FIRST_VALUE(subq_25.ds__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_23.metric_time__day) OVER ( + , FIRST_VALUE(subq_25.metric_time__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_23.user) OVER ( + , FIRST_VALUE(subq_25.user) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_26.mf_internal_uuid AS mf_internal_uuid - , subq_26.buys AS buys + , subq_28.mf_internal_uuid AS mf_internal_uuid + , subq_28.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + ds__day + , metric_time__day + , subq_23.user + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_23 + WHERE metric_time__day = '2020-01-01' + ) subq_25 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -92,23 +101,23 @@ FROM ( , 1 AS buys , UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_26 + ) subq_28 ON ( - subq_23.user = subq_26.user + subq_25.user = subq_28.user ) AND ( ( - subq_23.ds__day <= subq_26.ds__day + subq_25.ds__day <= subq_28.ds__day ) AND ( - subq_23.ds__day > DATEADD(day, -7, subq_26.ds__day) + subq_25.ds__day > DATEADD(day, -7, subq_28.ds__day) ) ) - ) subq_27 + ) subq_29 GROUP BY metric_time__day - ) subq_30 + ) subq_32 ON - subq_20.metric_time__day = subq_30.metric_time__day + subq_21.metric_time__day = subq_32.metric_time__day GROUP BY - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) -) subq_31 + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) +) subq_33 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_window_and_time_constraint__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_window_and_time_constraint__plan0.sql index d411808964..9203cabbf1 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_window_and_time_constraint__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_window_and_time_constraint__plan0.sql @@ -1,15 +1,15 @@ -- Compute Metrics via Expressions SELECT - subq_17.metric_time__day - , subq_17.visit__referrer_id - , CAST(subq_17.buys AS DOUBLE) / CAST(NULLIF(subq_17.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days + subq_18.metric_time__day + , subq_18.visit__referrer_id + , CAST(subq_18.buys AS DOUBLE) / CAST(NULLIF(subq_18.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_5.metric_time__day, subq_16.metric_time__day) AS metric_time__day - , COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_5.metric_time__day, subq_17.metric_time__day) AS metric_time__day + , COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) AS visit__referrer_id , MAX(subq_5.visits) AS visits - , MAX(subq_16.buys) AS buys + , MAX(subq_17.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -164,289 +164,335 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_15.metric_time__day - , subq_15.visit__referrer_id - , SUM(subq_15.buys) AS buys + subq_16.metric_time__day + , subq_16.visit__referrer_id + , SUM(subq_16.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] SELECT - subq_14.metric_time__day - , subq_14.visit__referrer_id - , subq_14.buys + subq_15.metric_time__day + , subq_15.visit__referrer_id + , subq_15.buys FROM ( -- Find conversions for user within the range of 7 day SELECT - subq_13.ds__day - , subq_13.metric_time__day - , subq_13.user - , subq_13.visit__referrer_id - , subq_13.buys - , subq_13.visits + subq_14.ds__day + , subq_14.metric_time__day + , subq_14.user + , subq_14.visit__referrer_id + , subq_14.buys + , subq_14.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_9.visits) OVER ( + FIRST_VALUE(subq_10.visits) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_9.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_10.visit__referrer_id) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_9.ds__day) OVER ( + , FIRST_VALUE(subq_10.ds__day) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_9.metric_time__day) OVER ( + , FIRST_VALUE(subq_10.metric_time__day) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_9.user) OVER ( + , FIRST_VALUE(subq_10.user) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_12.mf_internal_uuid AS mf_internal_uuid - , subq_12.buys AS buys + , subq_13.mf_internal_uuid AS mf_internal_uuid + , subq_13.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_8.ds__day - , subq_8.metric_time__day - , subq_8.user - , subq_8.visit__referrer_id - , subq_8.visits + subq_9.ds__day + , subq_9.metric_time__day + , subq_9.user + , subq_9.visit__referrer_id + , subq_9.visits FROM ( - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE SELECT - subq_7.ds__day - , subq_7.ds__week - , subq_7.ds__month - , subq_7.ds__quarter - , subq_7.ds__year - , subq_7.ds__extract_year - , subq_7.ds__extract_quarter - , subq_7.ds__extract_month - , subq_7.ds__extract_day - , subq_7.ds__extract_dow - , subq_7.ds__extract_doy - , subq_7.visit__ds__day - , subq_7.visit__ds__week - , subq_7.visit__ds__month - , subq_7.visit__ds__quarter - , subq_7.visit__ds__year - , subq_7.visit__ds__extract_year - , subq_7.visit__ds__extract_quarter - , subq_7.visit__ds__extract_month - , subq_7.visit__ds__extract_day - , subq_7.visit__ds__extract_dow - , subq_7.visit__ds__extract_doy - , subq_7.metric_time__day - , subq_7.metric_time__week - , subq_7.metric_time__month - , subq_7.metric_time__quarter - , subq_7.metric_time__year - , subq_7.metric_time__extract_year - , subq_7.metric_time__extract_quarter - , subq_7.metric_time__extract_month - , subq_7.metric_time__extract_day - , subq_7.metric_time__extract_dow - , subq_7.metric_time__extract_doy - , subq_7.user - , subq_7.session - , subq_7.visit__user - , subq_7.visit__session - , subq_7.referrer_id - , subq_7.visit__referrer_id - , subq_7.visits - , subq_7.visitors + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.visit__ds__day + , subq_8.visit__ds__week + , subq_8.visit__ds__month + , subq_8.visit__ds__quarter + , subq_8.visit__ds__year + , subq_8.visit__ds__extract_year + , subq_8.visit__ds__extract_quarter + , subq_8.visit__ds__extract_month + , subq_8.visit__ds__extract_day + , subq_8.visit__ds__extract_dow + , subq_8.visit__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session + , subq_8.visit__user + , subq_8.visit__session + , subq_8.referrer_id + , subq_8.visit__referrer_id + , subq_8.visits + , subq_8.visitors FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] SELECT - subq_6.ds__day - , subq_6.ds__week - , subq_6.ds__month - , subq_6.ds__quarter - , subq_6.ds__year - , subq_6.ds__extract_year - , subq_6.ds__extract_quarter - , subq_6.ds__extract_month - , subq_6.ds__extract_day - , subq_6.ds__extract_dow - , subq_6.ds__extract_doy - , subq_6.visit__ds__day - , subq_6.visit__ds__week - , subq_6.visit__ds__month - , subq_6.visit__ds__quarter - , subq_6.visit__ds__year - , subq_6.visit__ds__extract_year - , subq_6.visit__ds__extract_quarter - , subq_6.visit__ds__extract_month - , subq_6.visit__ds__extract_day - , subq_6.visit__ds__extract_dow - , subq_6.visit__ds__extract_doy - , subq_6.ds__day AS metric_time__day - , subq_6.ds__week AS metric_time__week - , subq_6.ds__month AS metric_time__month - , subq_6.ds__quarter AS metric_time__quarter - , subq_6.ds__year AS metric_time__year - , subq_6.ds__extract_year AS metric_time__extract_year - , subq_6.ds__extract_quarter AS metric_time__extract_quarter - , subq_6.ds__extract_month AS metric_time__extract_month - , subq_6.ds__extract_day AS metric_time__extract_day - , subq_6.ds__extract_dow AS metric_time__extract_dow - , subq_6.ds__extract_doy AS metric_time__extract_doy - , subq_6.user - , subq_6.session - , subq_6.visit__user - , subq_6.visit__session - , subq_6.referrer_id - , subq_6.visit__referrer_id - , subq_6.visits - , subq_6.visitors + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_6 - ) subq_7 - WHERE subq_7.metric_time__day BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_8 - ) subq_9 + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE subq_7.metric_time__day BETWEEN '2020-01-01' AND '2020-01-02' + ) subq_8 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_9 + ) subq_10 INNER JOIN ( -- Add column with generated UUID SELECT - subq_11.ds__day - , subq_11.ds__week - , subq_11.ds__month - , subq_11.ds__quarter - , subq_11.ds__year - , subq_11.ds__extract_year - , subq_11.ds__extract_quarter - , subq_11.ds__extract_month - , subq_11.ds__extract_day - , subq_11.ds__extract_dow - , subq_11.ds__extract_doy - , subq_11.buy__ds__day - , subq_11.buy__ds__week - , subq_11.buy__ds__month - , subq_11.buy__ds__quarter - , subq_11.buy__ds__year - , subq_11.buy__ds__extract_year - , subq_11.buy__ds__extract_quarter - , subq_11.buy__ds__extract_month - , subq_11.buy__ds__extract_day - , subq_11.buy__ds__extract_dow - , subq_11.buy__ds__extract_doy - , subq_11.metric_time__day - , subq_11.metric_time__week - , subq_11.metric_time__month - , subq_11.metric_time__quarter - , subq_11.metric_time__year - , subq_11.metric_time__extract_year - , subq_11.metric_time__extract_quarter - , subq_11.metric_time__extract_month - , subq_11.metric_time__extract_day - , subq_11.metric_time__extract_dow - , subq_11.metric_time__extract_doy - , subq_11.user - , subq_11.session_id - , subq_11.buy__user - , subq_11.buy__session_id - , subq_11.buys - , subq_11.buyers + subq_12.ds__day + , subq_12.ds__week + , subq_12.ds__month + , subq_12.ds__quarter + , subq_12.ds__year + , subq_12.ds__extract_year + , subq_12.ds__extract_quarter + , subq_12.ds__extract_month + , subq_12.ds__extract_day + , subq_12.ds__extract_dow + , subq_12.ds__extract_doy + , subq_12.buy__ds__day + , subq_12.buy__ds__week + , subq_12.buy__ds__month + , subq_12.buy__ds__quarter + , subq_12.buy__ds__year + , subq_12.buy__ds__extract_year + , subq_12.buy__ds__extract_quarter + , subq_12.buy__ds__extract_month + , subq_12.buy__ds__extract_day + , subq_12.buy__ds__extract_dow + , subq_12.buy__ds__extract_doy + , subq_12.metric_time__day + , subq_12.metric_time__week + , subq_12.metric_time__month + , subq_12.metric_time__quarter + , subq_12.metric_time__year + , subq_12.metric_time__extract_year + , subq_12.metric_time__extract_quarter + , subq_12.metric_time__extract_month + , subq_12.metric_time__extract_day + , subq_12.metric_time__extract_dow + , subq_12.metric_time__extract_doy + , subq_12.user + , subq_12.session_id + , subq_12.buy__user + , subq_12.buy__session_id + , subq_12.buys + , subq_12.buyers , UUID() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_10.ds__day - , subq_10.ds__week - , subq_10.ds__month - , subq_10.ds__quarter - , subq_10.ds__year - , subq_10.ds__extract_year - , subq_10.ds__extract_quarter - , subq_10.ds__extract_month - , subq_10.ds__extract_day - , subq_10.ds__extract_dow - , subq_10.ds__extract_doy - , subq_10.buy__ds__day - , subq_10.buy__ds__week - , subq_10.buy__ds__month - , subq_10.buy__ds__quarter - , subq_10.buy__ds__year - , subq_10.buy__ds__extract_year - , subq_10.buy__ds__extract_quarter - , subq_10.buy__ds__extract_month - , subq_10.buy__ds__extract_day - , subq_10.buy__ds__extract_dow - , subq_10.buy__ds__extract_doy - , subq_10.ds__day AS metric_time__day - , subq_10.ds__week AS metric_time__week - , subq_10.ds__month AS metric_time__month - , subq_10.ds__quarter AS metric_time__quarter - , subq_10.ds__year AS metric_time__year - , subq_10.ds__extract_year AS metric_time__extract_year - , subq_10.ds__extract_quarter AS metric_time__extract_quarter - , subq_10.ds__extract_month AS metric_time__extract_month - , subq_10.ds__extract_day AS metric_time__extract_day - , subq_10.ds__extract_dow AS metric_time__extract_dow - , subq_10.ds__extract_doy AS metric_time__extract_doy - , subq_10.user - , subq_10.session_id - , subq_10.buy__user - , subq_10.buy__session_id - , subq_10.buys - , subq_10.buyers + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.ds__day AS metric_time__day + , subq_11.ds__week AS metric_time__week + , subq_11.ds__month AS metric_time__month + , subq_11.ds__quarter AS metric_time__quarter + , subq_11.ds__year AS metric_time__year + , subq_11.ds__extract_year AS metric_time__extract_year + , subq_11.ds__extract_quarter AS metric_time__extract_quarter + , subq_11.ds__extract_month AS metric_time__extract_month + , subq_11.ds__extract_day AS metric_time__extract_day + , subq_11.ds__extract_dow AS metric_time__extract_dow + , subq_11.ds__extract_doy AS metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -479,33 +525,33 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_10 - ) subq_11 - ) subq_12 + ) subq_11 + ) subq_12 + ) subq_13 ON ( - subq_9.user = subq_12.user + subq_10.user = subq_13.user ) AND ( ( - subq_9.ds__day <= subq_12.ds__day + subq_10.ds__day <= subq_13.ds__day ) AND ( - subq_9.ds__day > DATEADD(day, -7, subq_12.ds__day) + subq_10.ds__day > DATEADD(day, -7, subq_13.ds__day) ) ) - ) subq_13 - ) subq_14 - ) subq_15 + ) subq_14 + ) subq_15 + ) subq_16 GROUP BY - subq_15.metric_time__day - , subq_15.visit__referrer_id - ) subq_16 + subq_16.metric_time__day + , subq_16.visit__referrer_id + ) subq_17 ON ( - subq_5.visit__referrer_id = subq_16.visit__referrer_id + subq_5.visit__referrer_id = subq_17.visit__referrer_id ) AND ( - subq_5.metric_time__day = subq_16.metric_time__day + subq_5.metric_time__day = subq_17.metric_time__day ) GROUP BY - COALESCE(subq_5.metric_time__day, subq_16.metric_time__day) - , COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) -) subq_17 + COALESCE(subq_5.metric_time__day, subq_17.metric_time__day) + , COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) +) subq_18 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql index c0a7cb56b3..f79d933d01 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql @@ -6,10 +6,10 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_23.metric_time__day, subq_34.metric_time__day) AS metric_time__day - , COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) AS visit__referrer_id - , MAX(subq_23.visits) AS visits - , MAX(subq_34.buys) AS buys + COALESCE(subq_24.metric_time__day, subq_36.metric_time__day) AS metric_time__day + , COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) AS visit__referrer_id + , MAX(subq_24.visits) AS visits + , MAX(subq_36.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -28,12 +28,12 @@ FROM ( , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_21 + ) subq_22 WHERE visit__referrer_id = 'ref_id_01' GROUP BY metric_time__day , visit__referrer_id - ) subq_23 + ) subq_24 FULL OUTER JOIN ( -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] @@ -45,62 +45,72 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_27.visits) OVER ( + FIRST_VALUE(subq_29.visits) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_27.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_29.visit__referrer_id) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_27.ds__day) OVER ( + , FIRST_VALUE(subq_29.ds__day) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_27.metric_time__day) OVER ( + , FIRST_VALUE(subq_29.metric_time__day) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_27.user) OVER ( + , FIRST_VALUE(subq_29.user) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_30.mf_internal_uuid AS mf_internal_uuid - , subq_30.buys AS buys + , subq_32.mf_internal_uuid AS mf_internal_uuid + , subq_32.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_27 + ds__day + , metric_time__day + , subq_27.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' + ) subq_27 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_29 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -111,29 +121,29 @@ FROM ( , 1 AS buys , UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_30 + ) subq_32 ON ( - subq_27.user = subq_30.user + subq_29.user = subq_32.user ) AND ( ( - subq_27.ds__day <= subq_30.ds__day + subq_29.ds__day <= subq_32.ds__day ) AND ( - subq_27.ds__day > DATEADD(day, -7, subq_30.ds__day) + subq_29.ds__day > DATEADD(day, -7, subq_32.ds__day) ) ) - ) subq_31 + ) subq_33 GROUP BY metric_time__day , visit__referrer_id - ) subq_34 + ) subq_36 ON ( - subq_23.visit__referrer_id = subq_34.visit__referrer_id + subq_24.visit__referrer_id = subq_36.visit__referrer_id ) AND ( - subq_23.metric_time__day = subq_34.metric_time__day + subq_24.metric_time__day = subq_36.metric_time__day ) GROUP BY - COALESCE(subq_23.metric_time__day, subq_34.metric_time__day) - , COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) -) subq_35 + COALESCE(subq_24.metric_time__day, subq_36.metric_time__day) + , COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) +) subq_37 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric__plan0.sql index 58ba934733..094ffea7ad 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric__plan0.sql @@ -1,13 +1,13 @@ -- Compute Metrics via Expressions SELECT - subq_15.metric_time__day - , CAST(subq_15.buys AS DOUBLE) / CAST(NULLIF(subq_15.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate + subq_16.metric_time__day + , CAST(subq_16.buys AS DOUBLE) / CAST(NULLIF(subq_16.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) AS metric_time__day + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) AS metric_time__day , MAX(subq_4.visits) AS visits - , MAX(subq_14.buys) AS buys + , MAX(subq_15.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -112,231 +112,277 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_13.metric_time__day - , SUM(subq_13.buys) AS buys + subq_14.metric_time__day + , SUM(subq_14.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'metric_time__day'] SELECT - subq_12.metric_time__day - , subq_12.buys + subq_13.metric_time__day + , subq_13.buys FROM ( -- Find conversions for user within the range of INF SELECT - subq_11.ds__day - , subq_11.metric_time__day - , subq_11.user - , subq_11.buys - , subq_11.visits + subq_12.ds__day + , subq_12.metric_time__day + , subq_12.user + , subq_12.buys + , subq_12.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_7.visits) OVER ( + FIRST_VALUE(subq_8.visits) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_7.ds__day) OVER ( + , FIRST_VALUE(subq_8.ds__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_7.metric_time__day) OVER ( + , FIRST_VALUE(subq_8.metric_time__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_7.user) OVER ( + , FIRST_VALUE(subq_8.user) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_10.mf_internal_uuid AS mf_internal_uuid - , subq_10.buys AS buys + , subq_11.mf_internal_uuid AS mf_internal_uuid + , subq_11.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_6.ds__day - , subq_6.metric_time__day - , subq_6.user - , subq_6.visits + subq_7.ds__day + , subq_7.metric_time__day + , subq_7.user + , subq_7.visits FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE SELECT - subq_5.ds__day - , subq_5.ds__week - , subq_5.ds__month - , subq_5.ds__quarter - , subq_5.ds__year - , subq_5.ds__extract_year - , subq_5.ds__extract_quarter - , subq_5.ds__extract_month - , subq_5.ds__extract_day - , subq_5.ds__extract_dow - , subq_5.ds__extract_doy - , subq_5.visit__ds__day - , subq_5.visit__ds__week - , subq_5.visit__ds__month - , subq_5.visit__ds__quarter - , subq_5.visit__ds__year - , subq_5.visit__ds__extract_year - , subq_5.visit__ds__extract_quarter - , subq_5.visit__ds__extract_month - , subq_5.visit__ds__extract_day - , subq_5.visit__ds__extract_dow - , subq_5.visit__ds__extract_doy - , subq_5.ds__day AS metric_time__day - , subq_5.ds__week AS metric_time__week - , subq_5.ds__month AS metric_time__month - , subq_5.ds__quarter AS metric_time__quarter - , subq_5.ds__year AS metric_time__year - , subq_5.ds__extract_year AS metric_time__extract_year - , subq_5.ds__extract_quarter AS metric_time__extract_quarter - , subq_5.ds__extract_month AS metric_time__extract_month - , subq_5.ds__extract_day AS metric_time__extract_day - , subq_5.ds__extract_dow AS metric_time__extract_dow - , subq_5.ds__extract_doy AS metric_time__extract_doy - , subq_5.user - , subq_5.session - , subq_5.visit__user - , subq_5.visit__session - , subq_5.referrer_id - , subq_5.visit__referrer_id - , subq_5.visits - , subq_5.visitors + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.metric_time__day + , subq_6.metric_time__week + , subq_6.metric_time__month + , subq_6.metric_time__quarter + , subq_6.metric_time__year + , subq_6.metric_time__extract_year + , subq_6.metric_time__extract_quarter + , subq_6.metric_time__extract_month + , subq_6.metric_time__extract_day + , subq_6.metric_time__extract_dow + , subq_6.metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_5 - ) subq_6 - ) subq_7 + subq_5.ds__day + , subq_5.ds__week + , subq_5.ds__month + , subq_5.ds__quarter + , subq_5.ds__year + , subq_5.ds__extract_year + , subq_5.ds__extract_quarter + , subq_5.ds__extract_month + , subq_5.ds__extract_day + , subq_5.ds__extract_dow + , subq_5.ds__extract_doy + , subq_5.visit__ds__day + , subq_5.visit__ds__week + , subq_5.visit__ds__month + , subq_5.visit__ds__quarter + , subq_5.visit__ds__year + , subq_5.visit__ds__extract_year + , subq_5.visit__ds__extract_quarter + , subq_5.visit__ds__extract_month + , subq_5.visit__ds__extract_day + , subq_5.visit__ds__extract_dow + , subq_5.visit__ds__extract_doy + , subq_5.ds__day AS metric_time__day + , subq_5.ds__week AS metric_time__week + , subq_5.ds__month AS metric_time__month + , subq_5.ds__quarter AS metric_time__quarter + , subq_5.ds__year AS metric_time__year + , subq_5.ds__extract_year AS metric_time__extract_year + , subq_5.ds__extract_quarter AS metric_time__extract_quarter + , subq_5.ds__extract_month AS metric_time__extract_month + , subq_5.ds__extract_day AS metric_time__extract_day + , subq_5.ds__extract_dow AS metric_time__extract_dow + , subq_5.ds__extract_doy AS metric_time__extract_doy + , subq_5.user + , subq_5.session + , subq_5.visit__user + , subq_5.visit__session + , subq_5.referrer_id + , subq_5.visit__referrer_id + , subq_5.visits + , subq_5.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_5 + ) subq_6 + WHERE metric_time__day = '2020-01-01' + ) subq_7 + ) subq_8 INNER JOIN ( -- Add column with generated UUID SELECT - subq_9.ds__day - , subq_9.ds__week - , subq_9.ds__month - , subq_9.ds__quarter - , subq_9.ds__year - , subq_9.ds__extract_year - , subq_9.ds__extract_quarter - , subq_9.ds__extract_month - , subq_9.ds__extract_day - , subq_9.ds__extract_dow - , subq_9.ds__extract_doy - , subq_9.buy__ds__day - , subq_9.buy__ds__week - , subq_9.buy__ds__month - , subq_9.buy__ds__quarter - , subq_9.buy__ds__year - , subq_9.buy__ds__extract_year - , subq_9.buy__ds__extract_quarter - , subq_9.buy__ds__extract_month - , subq_9.buy__ds__extract_day - , subq_9.buy__ds__extract_dow - , subq_9.buy__ds__extract_doy - , subq_9.metric_time__day - , subq_9.metric_time__week - , subq_9.metric_time__month - , subq_9.metric_time__quarter - , subq_9.metric_time__year - , subq_9.metric_time__extract_year - , subq_9.metric_time__extract_quarter - , subq_9.metric_time__extract_month - , subq_9.metric_time__extract_day - , subq_9.metric_time__extract_dow - , subq_9.metric_time__extract_doy - , subq_9.user - , subq_9.session_id - , subq_9.buy__user - , subq_9.buy__session_id - , subq_9.buys - , subq_9.buyers + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.metric_time__day + , subq_10.metric_time__week + , subq_10.metric_time__month + , subq_10.metric_time__quarter + , subq_10.metric_time__year + , subq_10.metric_time__extract_year + , subq_10.metric_time__extract_quarter + , subq_10.metric_time__extract_month + , subq_10.metric_time__extract_day + , subq_10.metric_time__extract_dow + , subq_10.metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_8.ds__day - , subq_8.ds__week - , subq_8.ds__month - , subq_8.ds__quarter - , subq_8.ds__year - , subq_8.ds__extract_year - , subq_8.ds__extract_quarter - , subq_8.ds__extract_month - , subq_8.ds__extract_day - , subq_8.ds__extract_dow - , subq_8.ds__extract_doy - , subq_8.buy__ds__day - , subq_8.buy__ds__week - , subq_8.buy__ds__month - , subq_8.buy__ds__quarter - , subq_8.buy__ds__year - , subq_8.buy__ds__extract_year - , subq_8.buy__ds__extract_quarter - , subq_8.buy__ds__extract_month - , subq_8.buy__ds__extract_day - , subq_8.buy__ds__extract_dow - , subq_8.buy__ds__extract_doy - , subq_8.ds__day AS metric_time__day - , subq_8.ds__week AS metric_time__week - , subq_8.ds__month AS metric_time__month - , subq_8.ds__quarter AS metric_time__quarter - , subq_8.ds__year AS metric_time__year - , subq_8.ds__extract_year AS metric_time__extract_year - , subq_8.ds__extract_quarter AS metric_time__extract_quarter - , subq_8.ds__extract_month AS metric_time__extract_month - , subq_8.ds__extract_day AS metric_time__extract_day - , subq_8.ds__extract_dow AS metric_time__extract_dow - , subq_8.ds__extract_doy AS metric_time__extract_doy - , subq_8.user - , subq_8.session_id - , subq_8.buy__user - , subq_8.buy__session_id - , subq_8.buys - , subq_8.buyers + subq_9.ds__day + , subq_9.ds__week + , subq_9.ds__month + , subq_9.ds__quarter + , subq_9.ds__year + , subq_9.ds__extract_year + , subq_9.ds__extract_quarter + , subq_9.ds__extract_month + , subq_9.ds__extract_day + , subq_9.ds__extract_dow + , subq_9.ds__extract_doy + , subq_9.buy__ds__day + , subq_9.buy__ds__week + , subq_9.buy__ds__month + , subq_9.buy__ds__quarter + , subq_9.buy__ds__year + , subq_9.buy__ds__extract_year + , subq_9.buy__ds__extract_quarter + , subq_9.buy__ds__extract_month + , subq_9.buy__ds__extract_day + , subq_9.buy__ds__extract_dow + , subq_9.buy__ds__extract_doy + , subq_9.ds__day AS metric_time__day + , subq_9.ds__week AS metric_time__week + , subq_9.ds__month AS metric_time__month + , subq_9.ds__quarter AS metric_time__quarter + , subq_9.ds__year AS metric_time__year + , subq_9.ds__extract_year AS metric_time__extract_year + , subq_9.ds__extract_quarter AS metric_time__extract_quarter + , subq_9.ds__extract_month AS metric_time__extract_month + , subq_9.ds__extract_day AS metric_time__extract_day + , subq_9.ds__extract_dow AS metric_time__extract_dow + , subq_9.ds__extract_doy AS metric_time__extract_doy + , subq_9.user + , subq_9.session_id + , subq_9.buy__user + , subq_9.buy__session_id + , subq_9.buys + , subq_9.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -369,23 +415,23 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_8 - ) subq_9 - ) subq_10 + ) subq_9 + ) subq_10 + ) subq_11 ON ( - subq_7.user = subq_10.user + subq_8.user = subq_11.user ) AND ( - (subq_7.ds__day <= subq_10.ds__day) + (subq_8.ds__day <= subq_11.ds__day) ) - ) subq_11 - ) subq_12 - ) subq_13 + ) subq_12 + ) subq_13 + ) subq_14 GROUP BY - subq_13.metric_time__day - ) subq_14 + subq_14.metric_time__day + ) subq_15 ON - subq_4.metric_time__day = subq_14.metric_time__day + subq_4.metric_time__day = subq_15.metric_time__day GROUP BY - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) -) subq_15 + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) +) subq_16 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric__plan0_optimized.sql index 8a91583adb..8928b1f4c0 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric__plan0_optimized.sql @@ -5,9 +5,9 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_20.visits) AS visits - , MAX(subq_30.buys) AS buys + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , MAX(subq_21.visits) AS visits + , MAX(subq_32.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -22,11 +22,11 @@ FROM ( DATE_TRUNC('day', ds) AS metric_time__day , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_18 + ) subq_19 WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__day - ) subq_20 + ) subq_21 FULL OUTER JOIN ( -- Find conversions for user within the range of INF -- Pass Only Elements: ['buys', 'metric_time__day'] @@ -37,51 +37,60 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_23.visits) OVER ( + FIRST_VALUE(subq_25.visits) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_23.ds__day) OVER ( + , FIRST_VALUE(subq_25.ds__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_23.metric_time__day) OVER ( + , FIRST_VALUE(subq_25.metric_time__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_23.user) OVER ( + , FIRST_VALUE(subq_25.user) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_26.mf_internal_uuid AS mf_internal_uuid - , subq_26.buys AS buys + , subq_28.mf_internal_uuid AS mf_internal_uuid + , subq_28.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + ds__day + , metric_time__day + , subq_23.user + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_23 + WHERE metric_time__day = '2020-01-01' + ) subq_25 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -92,19 +101,19 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_26 + ) subq_28 ON ( - subq_23.user = subq_26.user + subq_25.user = subq_28.user ) AND ( - (subq_23.ds__day <= subq_26.ds__day) + (subq_25.ds__day <= subq_28.ds__day) ) - ) subq_27 + ) subq_29 GROUP BY metric_time__day - ) subq_30 + ) subq_32 ON - subq_20.metric_time__day = subq_30.metric_time__day + subq_21.metric_time__day = subq_32.metric_time__day GROUP BY - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) -) subq_31 + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) +) subq_33 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_categorical_filter__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_categorical_filter__plan0.sql index fc3d6d87f5..4344ad97b6 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_categorical_filter__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_categorical_filter__plan0.sql @@ -1,15 +1,15 @@ -- Compute Metrics via Expressions SELECT - subq_15.metric_time__day - , subq_15.visit__referrer_id - , CAST(subq_15.buys AS DOUBLE) / CAST(NULLIF(subq_15.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate + subq_16.metric_time__day + , subq_16.visit__referrer_id + , CAST(subq_16.buys AS DOUBLE) / CAST(NULLIF(subq_16.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) AS metric_time__day - , COALESCE(subq_4.visit__referrer_id, subq_14.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) AS metric_time__day + , COALESCE(subq_4.visit__referrer_id, subq_15.visit__referrer_id) AS visit__referrer_id , MAX(subq_4.visits) AS visits - , MAX(subq_14.buys) AS buys + , MAX(subq_15.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -118,243 +118,289 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_13.metric_time__day - , subq_13.visit__referrer_id - , SUM(subq_13.buys) AS buys + subq_14.metric_time__day + , subq_14.visit__referrer_id + , SUM(subq_14.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] SELECT - subq_12.metric_time__day - , subq_12.visit__referrer_id - , subq_12.buys + subq_13.metric_time__day + , subq_13.visit__referrer_id + , subq_13.buys FROM ( -- Find conversions for user within the range of INF SELECT - subq_11.ds__day - , subq_11.metric_time__day - , subq_11.user - , subq_11.visit__referrer_id - , subq_11.buys - , subq_11.visits + subq_12.ds__day + , subq_12.metric_time__day + , subq_12.user + , subq_12.visit__referrer_id + , subq_12.buys + , subq_12.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_7.visits) OVER ( + FIRST_VALUE(subq_8.visits) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_7.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_8.visit__referrer_id) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_7.ds__day) OVER ( + , FIRST_VALUE(subq_8.ds__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_7.metric_time__day) OVER ( + , FIRST_VALUE(subq_8.metric_time__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_7.user) OVER ( + , FIRST_VALUE(subq_8.user) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_10.mf_internal_uuid AS mf_internal_uuid - , subq_10.buys AS buys + , subq_11.mf_internal_uuid AS mf_internal_uuid + , subq_11.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_6.ds__day - , subq_6.metric_time__day - , subq_6.user - , subq_6.visit__referrer_id - , subq_6.visits + subq_7.ds__day + , subq_7.metric_time__day + , subq_7.user + , subq_7.visit__referrer_id + , subq_7.visits FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE SELECT - subq_5.ds__day - , subq_5.ds__week - , subq_5.ds__month - , subq_5.ds__quarter - , subq_5.ds__year - , subq_5.ds__extract_year - , subq_5.ds__extract_quarter - , subq_5.ds__extract_month - , subq_5.ds__extract_day - , subq_5.ds__extract_dow - , subq_5.ds__extract_doy - , subq_5.visit__ds__day - , subq_5.visit__ds__week - , subq_5.visit__ds__month - , subq_5.visit__ds__quarter - , subq_5.visit__ds__year - , subq_5.visit__ds__extract_year - , subq_5.visit__ds__extract_quarter - , subq_5.visit__ds__extract_month - , subq_5.visit__ds__extract_day - , subq_5.visit__ds__extract_dow - , subq_5.visit__ds__extract_doy - , subq_5.ds__day AS metric_time__day - , subq_5.ds__week AS metric_time__week - , subq_5.ds__month AS metric_time__month - , subq_5.ds__quarter AS metric_time__quarter - , subq_5.ds__year AS metric_time__year - , subq_5.ds__extract_year AS metric_time__extract_year - , subq_5.ds__extract_quarter AS metric_time__extract_quarter - , subq_5.ds__extract_month AS metric_time__extract_month - , subq_5.ds__extract_day AS metric_time__extract_day - , subq_5.ds__extract_dow AS metric_time__extract_dow - , subq_5.ds__extract_doy AS metric_time__extract_doy - , subq_5.user - , subq_5.session - , subq_5.visit__user - , subq_5.visit__session - , subq_5.referrer_id - , subq_5.visit__referrer_id - , subq_5.visits - , subq_5.visitors + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.metric_time__day + , subq_6.metric_time__week + , subq_6.metric_time__month + , subq_6.metric_time__quarter + , subq_6.metric_time__year + , subq_6.metric_time__extract_year + , subq_6.metric_time__extract_quarter + , subq_6.metric_time__extract_month + , subq_6.metric_time__extract_day + , subq_6.metric_time__extract_dow + , subq_6.metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_5 - ) subq_6 - ) subq_7 + subq_5.ds__day + , subq_5.ds__week + , subq_5.ds__month + , subq_5.ds__quarter + , subq_5.ds__year + , subq_5.ds__extract_year + , subq_5.ds__extract_quarter + , subq_5.ds__extract_month + , subq_5.ds__extract_day + , subq_5.ds__extract_dow + , subq_5.ds__extract_doy + , subq_5.visit__ds__day + , subq_5.visit__ds__week + , subq_5.visit__ds__month + , subq_5.visit__ds__quarter + , subq_5.visit__ds__year + , subq_5.visit__ds__extract_year + , subq_5.visit__ds__extract_quarter + , subq_5.visit__ds__extract_month + , subq_5.visit__ds__extract_day + , subq_5.visit__ds__extract_dow + , subq_5.visit__ds__extract_doy + , subq_5.ds__day AS metric_time__day + , subq_5.ds__week AS metric_time__week + , subq_5.ds__month AS metric_time__month + , subq_5.ds__quarter AS metric_time__quarter + , subq_5.ds__year AS metric_time__year + , subq_5.ds__extract_year AS metric_time__extract_year + , subq_5.ds__extract_quarter AS metric_time__extract_quarter + , subq_5.ds__extract_month AS metric_time__extract_month + , subq_5.ds__extract_day AS metric_time__extract_day + , subq_5.ds__extract_dow AS metric_time__extract_dow + , subq_5.ds__extract_doy AS metric_time__extract_doy + , subq_5.user + , subq_5.session + , subq_5.visit__user + , subq_5.visit__session + , subq_5.referrer_id + , subq_5.visit__referrer_id + , subq_5.visits + , subq_5.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_5 + ) subq_6 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_7 + ) subq_8 INNER JOIN ( -- Add column with generated UUID SELECT - subq_9.ds__day - , subq_9.ds__week - , subq_9.ds__month - , subq_9.ds__quarter - , subq_9.ds__year - , subq_9.ds__extract_year - , subq_9.ds__extract_quarter - , subq_9.ds__extract_month - , subq_9.ds__extract_day - , subq_9.ds__extract_dow - , subq_9.ds__extract_doy - , subq_9.buy__ds__day - , subq_9.buy__ds__week - , subq_9.buy__ds__month - , subq_9.buy__ds__quarter - , subq_9.buy__ds__year - , subq_9.buy__ds__extract_year - , subq_9.buy__ds__extract_quarter - , subq_9.buy__ds__extract_month - , subq_9.buy__ds__extract_day - , subq_9.buy__ds__extract_dow - , subq_9.buy__ds__extract_doy - , subq_9.metric_time__day - , subq_9.metric_time__week - , subq_9.metric_time__month - , subq_9.metric_time__quarter - , subq_9.metric_time__year - , subq_9.metric_time__extract_year - , subq_9.metric_time__extract_quarter - , subq_9.metric_time__extract_month - , subq_9.metric_time__extract_day - , subq_9.metric_time__extract_dow - , subq_9.metric_time__extract_doy - , subq_9.user - , subq_9.session_id - , subq_9.buy__user - , subq_9.buy__session_id - , subq_9.buys - , subq_9.buyers + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.metric_time__day + , subq_10.metric_time__week + , subq_10.metric_time__month + , subq_10.metric_time__quarter + , subq_10.metric_time__year + , subq_10.metric_time__extract_year + , subq_10.metric_time__extract_quarter + , subq_10.metric_time__extract_month + , subq_10.metric_time__extract_day + , subq_10.metric_time__extract_dow + , subq_10.metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_8.ds__day - , subq_8.ds__week - , subq_8.ds__month - , subq_8.ds__quarter - , subq_8.ds__year - , subq_8.ds__extract_year - , subq_8.ds__extract_quarter - , subq_8.ds__extract_month - , subq_8.ds__extract_day - , subq_8.ds__extract_dow - , subq_8.ds__extract_doy - , subq_8.buy__ds__day - , subq_8.buy__ds__week - , subq_8.buy__ds__month - , subq_8.buy__ds__quarter - , subq_8.buy__ds__year - , subq_8.buy__ds__extract_year - , subq_8.buy__ds__extract_quarter - , subq_8.buy__ds__extract_month - , subq_8.buy__ds__extract_day - , subq_8.buy__ds__extract_dow - , subq_8.buy__ds__extract_doy - , subq_8.ds__day AS metric_time__day - , subq_8.ds__week AS metric_time__week - , subq_8.ds__month AS metric_time__month - , subq_8.ds__quarter AS metric_time__quarter - , subq_8.ds__year AS metric_time__year - , subq_8.ds__extract_year AS metric_time__extract_year - , subq_8.ds__extract_quarter AS metric_time__extract_quarter - , subq_8.ds__extract_month AS metric_time__extract_month - , subq_8.ds__extract_day AS metric_time__extract_day - , subq_8.ds__extract_dow AS metric_time__extract_dow - , subq_8.ds__extract_doy AS metric_time__extract_doy - , subq_8.user - , subq_8.session_id - , subq_8.buy__user - , subq_8.buy__session_id - , subq_8.buys - , subq_8.buyers + subq_9.ds__day + , subq_9.ds__week + , subq_9.ds__month + , subq_9.ds__quarter + , subq_9.ds__year + , subq_9.ds__extract_year + , subq_9.ds__extract_quarter + , subq_9.ds__extract_month + , subq_9.ds__extract_day + , subq_9.ds__extract_dow + , subq_9.ds__extract_doy + , subq_9.buy__ds__day + , subq_9.buy__ds__week + , subq_9.buy__ds__month + , subq_9.buy__ds__quarter + , subq_9.buy__ds__year + , subq_9.buy__ds__extract_year + , subq_9.buy__ds__extract_quarter + , subq_9.buy__ds__extract_month + , subq_9.buy__ds__extract_day + , subq_9.buy__ds__extract_dow + , subq_9.buy__ds__extract_doy + , subq_9.ds__day AS metric_time__day + , subq_9.ds__week AS metric_time__week + , subq_9.ds__month AS metric_time__month + , subq_9.ds__quarter AS metric_time__quarter + , subq_9.ds__year AS metric_time__year + , subq_9.ds__extract_year AS metric_time__extract_year + , subq_9.ds__extract_quarter AS metric_time__extract_quarter + , subq_9.ds__extract_month AS metric_time__extract_month + , subq_9.ds__extract_day AS metric_time__extract_day + , subq_9.ds__extract_dow AS metric_time__extract_dow + , subq_9.ds__extract_doy AS metric_time__extract_doy + , subq_9.user + , subq_9.session_id + , subq_9.buy__user + , subq_9.buy__session_id + , subq_9.buys + , subq_9.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -387,29 +433,29 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_8 - ) subq_9 - ) subq_10 + ) subq_9 + ) subq_10 + ) subq_11 ON ( - subq_7.user = subq_10.user + subq_8.user = subq_11.user ) AND ( - (subq_7.ds__day <= subq_10.ds__day) + (subq_8.ds__day <= subq_11.ds__day) ) - ) subq_11 - ) subq_12 - ) subq_13 + ) subq_12 + ) subq_13 + ) subq_14 GROUP BY - subq_13.metric_time__day - , subq_13.visit__referrer_id - ) subq_14 + subq_14.metric_time__day + , subq_14.visit__referrer_id + ) subq_15 ON ( - subq_4.visit__referrer_id = subq_14.visit__referrer_id + subq_4.visit__referrer_id = subq_15.visit__referrer_id ) AND ( - subq_4.metric_time__day = subq_14.metric_time__day + subq_4.metric_time__day = subq_15.metric_time__day ) GROUP BY - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) - , COALESCE(subq_4.visit__referrer_id, subq_14.visit__referrer_id) -) subq_15 + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) + , COALESCE(subq_4.visit__referrer_id, subq_15.visit__referrer_id) +) subq_16 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_categorical_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_categorical_filter__plan0_optimized.sql index 6c179cce7f..f5428faea0 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_categorical_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_categorical_filter__plan0_optimized.sql @@ -6,10 +6,10 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , COALESCE(subq_20.visit__referrer_id, subq_30.visit__referrer_id) AS visit__referrer_id - , MAX(subq_20.visits) AS visits - , MAX(subq_30.buys) AS buys + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , COALESCE(subq_21.visit__referrer_id, subq_32.visit__referrer_id) AS visit__referrer_id + , MAX(subq_21.visits) AS visits + , MAX(subq_32.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -26,12 +26,12 @@ FROM ( , referrer_id AS visit__referrer_id , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_18 + ) subq_19 WHERE visit__referrer_id = 'ref_id_01' GROUP BY metric_time__day , visit__referrer_id - ) subq_20 + ) subq_21 FULL OUTER JOIN ( -- Find conversions for user within the range of INF -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] @@ -43,60 +43,70 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_23.visits) OVER ( + FIRST_VALUE(subq_25.visits) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_23.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_25.visit__referrer_id) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_23.ds__day) OVER ( + , FIRST_VALUE(subq_25.ds__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_23.metric_time__day) OVER ( + , FIRST_VALUE(subq_25.metric_time__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_23.user) OVER ( + , FIRST_VALUE(subq_25.user) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_26.mf_internal_uuid AS mf_internal_uuid - , subq_26.buys AS buys + , subq_28.mf_internal_uuid AS mf_internal_uuid + , subq_28.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + ds__day + , metric_time__day + , subq_23.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_23 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_25 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -107,25 +117,25 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_26 + ) subq_28 ON ( - subq_23.user = subq_26.user + subq_25.user = subq_28.user ) AND ( - (subq_23.ds__day <= subq_26.ds__day) + (subq_25.ds__day <= subq_28.ds__day) ) - ) subq_27 + ) subq_29 GROUP BY metric_time__day , visit__referrer_id - ) subq_30 + ) subq_32 ON ( - subq_20.visit__referrer_id = subq_30.visit__referrer_id + subq_21.visit__referrer_id = subq_32.visit__referrer_id ) AND ( - subq_20.metric_time__day = subq_30.metric_time__day + subq_21.metric_time__day = subq_32.metric_time__day ) GROUP BY - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) - , COALESCE(subq_20.visit__referrer_id, subq_30.visit__referrer_id) -) subq_31 + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) + , COALESCE(subq_21.visit__referrer_id, subq_32.visit__referrer_id) +) subq_33 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_filter__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_filter__plan0.sql new file mode 100644 index 0000000000..32676e2ba3 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_filter__plan0.sql @@ -0,0 +1,429 @@ +-- Compute Metrics via Expressions +SELECT + CAST(subq_17.buys AS DOUBLE) / CAST(NULLIF(subq_17.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_5.visits) AS visits + , MAX(subq_16.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_4.visits) AS visits + FROM ( + -- Pass Only Elements: ['visits',] + SELECT + subq_3.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_2.metric_time__day + , subq_2.visits + FROM ( + -- Pass Only Elements: ['visits', 'metric_time__day'] + SELECT + subq_1.metric_time__day + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_0 + ) subq_1 + ) subq_2 + WHERE metric_time__day = '2020-01-01' + ) subq_3 + ) subq_4 + ) subq_5 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_15.buys) AS buys + FROM ( + -- Pass Only Elements: ['buys',] + SELECT + subq_14.buys + FROM ( + -- Find conversions for user within the range of INF + SELECT + subq_13.ds__day + , subq_13.metric_time__day + , subq_13.user + , subq_13.buys + , subq_13.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_9.visits) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_9.ds__day) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_9.metric_time__day) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS metric_time__day + , FIRST_VALUE(subq_9.user) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_12.mf_internal_uuid AS mf_internal_uuid + , subq_12.buys AS buys + FROM ( + -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] + SELECT + subq_8.ds__day + , subq_8.metric_time__day + , subq_8.user + , subq_8.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE metric_time__day = '2020-01-01' + ) subq_8 + ) subq_9 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.metric_time__day + , subq_11.metric_time__week + , subq_11.metric_time__month + , subq_11.metric_time__quarter + , subq_11.metric_time__year + , subq_11.metric_time__extract_year + , subq_11.metric_time__extract_quarter + , subq_11.metric_time__extract_month + , subq_11.metric_time__extract_day + , subq_11.metric_time__extract_dow + , subq_11.metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.ds__day AS metric_time__day + , subq_10.ds__week AS metric_time__week + , subq_10.ds__month AS metric_time__month + , subq_10.ds__quarter AS metric_time__quarter + , subq_10.ds__year AS metric_time__year + , subq_10.ds__extract_year AS metric_time__extract_year + , subq_10.ds__extract_quarter AS metric_time__extract_quarter + , subq_10.ds__extract_month AS metric_time__extract_month + , subq_10.ds__extract_day AS metric_time__extract_day + , subq_10.ds__extract_dow AS metric_time__extract_dow + , subq_10.ds__extract_doy AS metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_28000.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM buys_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_28000.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day + , EXTRACT(isodow FROM buys_source_src_28000.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS buy__ds__extract_doy + , buys_source_src_28000.user_id AS user + , buys_source_src_28000.session_id + , buys_source_src_28000.user_id AS buy__user + , buys_source_src_28000.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_10 + ) subq_11 + ) subq_12 + ON + ( + subq_9.user = subq_12.user + ) AND ( + (subq_9.ds__day <= subq_12.ds__day) + ) + ) subq_13 + ) subq_14 + ) subq_15 + ) subq_16 +) subq_17 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_filter__plan0_optimized.sql new file mode 100644 index 0000000000..d33a723000 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_filter__plan0_optimized.sql @@ -0,0 +1,103 @@ +-- Combine Aggregated Outputs +-- Compute Metrics via Expressions +SELECT + CAST(MAX(subq_34.buys) AS DOUBLE) / CAST(NULLIF(MAX(subq_23.visits), 0) AS DOUBLE) AS visit_buy_conversion_rate +FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits',] + -- Aggregate Measures + SELECT + 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_20 + WHERE metric_time__day = '2020-01-01' +) subq_23 +CROSS JOIN ( + -- Find conversions for user within the range of INF + -- Pass Only Elements: ['buys',] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_27.visits) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_27.ds__day) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_27.metric_time__day) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS metric_time__day + , FIRST_VALUE(subq_27.user) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_30.mf_internal_uuid AS mf_internal_uuid + , subq_30.buys AS buys + FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] + SELECT + ds__day + , metric_time__day + , subq_25.user + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_25 + WHERE metric_time__day = '2020-01-01' + ) subq_27 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_30 + ON + ( + subq_27.user = subq_30.user + ) AND ( + (subq_27.ds__day <= subq_30.ds__day) + ) + ) subq_31 +) subq_34 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_filter_not_in_group_by__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_filter_not_in_group_by__plan0.sql new file mode 100644 index 0000000000..12b0fdb56b --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_filter_not_in_group_by__plan0.sql @@ -0,0 +1,433 @@ +-- Compute Metrics via Expressions +SELECT + subq_17.buys AS visit_buy_conversions +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_5.visits) AS visits + , COALESCE(MAX(subq_16.buys), 0) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_4.visits) AS visits + FROM ( + -- Pass Only Elements: ['visits',] + SELECT + subq_3.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_2.visit__referrer_id + , subq_2.visits + FROM ( + -- Pass Only Elements: ['visits', 'visit__referrer_id'] + SELECT + subq_1.visit__referrer_id + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_0 + ) subq_1 + ) subq_2 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_3 + ) subq_4 + ) subq_5 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_15.buys) AS buys + FROM ( + -- Pass Only Elements: ['buys',] + SELECT + subq_14.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_13.ds__day + , subq_13.user + , subq_13.visit__referrer_id + , subq_13.buys + , subq_13.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_9.visits) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_9.visit__referrer_id) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visit__referrer_id + , FIRST_VALUE(subq_9.ds__day) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_9.user) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_12.mf_internal_uuid AS mf_internal_uuid + , subq_12.buys AS buys + FROM ( + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] + SELECT + subq_8.ds__day + , subq_8.user + , subq_8.visit__referrer_id + , subq_8.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_8 + ) subq_9 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.metric_time__day + , subq_11.metric_time__week + , subq_11.metric_time__month + , subq_11.metric_time__quarter + , subq_11.metric_time__year + , subq_11.metric_time__extract_year + , subq_11.metric_time__extract_quarter + , subq_11.metric_time__extract_month + , subq_11.metric_time__extract_day + , subq_11.metric_time__extract_dow + , subq_11.metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.ds__day AS metric_time__day + , subq_10.ds__week AS metric_time__week + , subq_10.ds__month AS metric_time__month + , subq_10.ds__quarter AS metric_time__quarter + , subq_10.ds__year AS metric_time__year + , subq_10.ds__extract_year AS metric_time__extract_year + , subq_10.ds__extract_quarter AS metric_time__extract_quarter + , subq_10.ds__extract_month AS metric_time__extract_month + , subq_10.ds__extract_day AS metric_time__extract_day + , subq_10.ds__extract_dow AS metric_time__extract_dow + , subq_10.ds__extract_doy AS metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_28000.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM buys_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_28000.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day + , EXTRACT(isodow FROM buys_source_src_28000.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS buy__ds__extract_doy + , buys_source_src_28000.user_id AS user + , buys_source_src_28000.session_id + , buys_source_src_28000.user_id AS buy__user + , buys_source_src_28000.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_10 + ) subq_11 + ) subq_12 + ON + ( + subq_9.user = subq_12.user + ) AND ( + ( + subq_9.ds__day <= subq_12.ds__day + ) AND ( + subq_9.ds__day > subq_12.ds__day - INTERVAL 7 day + ) + ) + ) subq_13 + ) subq_14 + ) subq_15 + ) subq_16 +) subq_17 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_filter_not_in_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_filter_not_in_group_by__plan0_optimized.sql new file mode 100644 index 0000000000..ddfb2d2133 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_filter_not_in_group_by__plan0_optimized.sql @@ -0,0 +1,107 @@ +-- Combine Aggregated Outputs +-- Compute Metrics via Expressions +SELECT + COALESCE(MAX(subq_34.buys), 0) AS visit_buy_conversions +FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits',] + -- Aggregate Measures + SELECT + 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_20 + WHERE visit__referrer_id = 'ref_id_01' +) subq_23 +CROSS JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: ['buys',] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_27.visits) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_27.visit__referrer_id) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visit__referrer_id + , FIRST_VALUE(subq_27.ds__day) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_27.user) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_30.mf_internal_uuid AS mf_internal_uuid + , subq_30.buys AS buys + FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] + SELECT + ds__day + , subq_25.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_25 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_27 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_30 + ON + ( + subq_27.user = subq_30.user + ) AND ( + ( + subq_27.ds__day <= subq_30.ds__day + ) AND ( + subq_27.ds__day > subq_30.ds__day - INTERVAL 7 day + ) + ) + ) subq_31 +) subq_34 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_time_constraint__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_time_constraint__plan0.sql index b1e85e150b..68fa7ef3fb 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_time_constraint__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_time_constraint__plan0.sql @@ -1,13 +1,13 @@ -- Compute Metrics via Expressions SELECT - subq_17.visit__referrer_id - , CAST(subq_17.buys AS DOUBLE) / CAST(NULLIF(subq_17.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate + subq_18.visit__referrer_id + , CAST(subq_18.buys AS DOUBLE) / CAST(NULLIF(subq_18.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) AS visit__referrer_id , MAX(subq_5.visits) AS visits - , MAX(subq_16.buys) AS buys + , MAX(subq_17.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -158,277 +158,323 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_15.visit__referrer_id - , SUM(subq_15.buys) AS buys + subq_16.visit__referrer_id + , SUM(subq_16.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'visit__referrer_id'] SELECT - subq_14.visit__referrer_id - , subq_14.buys + subq_15.visit__referrer_id + , subq_15.buys FROM ( -- Find conversions for user within the range of INF SELECT - subq_13.ds__day - , subq_13.user - , subq_13.visit__referrer_id - , subq_13.buys - , subq_13.visits + subq_14.ds__day + , subq_14.user + , subq_14.visit__referrer_id + , subq_14.buys + , subq_14.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_9.visits) OVER ( + FIRST_VALUE(subq_10.visits) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_9.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_10.visit__referrer_id) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_9.ds__day) OVER ( + , FIRST_VALUE(subq_10.ds__day) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_9.user) OVER ( + , FIRST_VALUE(subq_10.user) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_12.mf_internal_uuid AS mf_internal_uuid - , subq_12.buys AS buys + , subq_13.mf_internal_uuid AS mf_internal_uuid + , subq_13.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] SELECT - subq_8.ds__day - , subq_8.user - , subq_8.visit__referrer_id - , subq_8.visits + subq_9.ds__day + , subq_9.user + , subq_9.visit__referrer_id + , subq_9.visits FROM ( - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE SELECT - subq_7.ds__day - , subq_7.ds__week - , subq_7.ds__month - , subq_7.ds__quarter - , subq_7.ds__year - , subq_7.ds__extract_year - , subq_7.ds__extract_quarter - , subq_7.ds__extract_month - , subq_7.ds__extract_day - , subq_7.ds__extract_dow - , subq_7.ds__extract_doy - , subq_7.visit__ds__day - , subq_7.visit__ds__week - , subq_7.visit__ds__month - , subq_7.visit__ds__quarter - , subq_7.visit__ds__year - , subq_7.visit__ds__extract_year - , subq_7.visit__ds__extract_quarter - , subq_7.visit__ds__extract_month - , subq_7.visit__ds__extract_day - , subq_7.visit__ds__extract_dow - , subq_7.visit__ds__extract_doy - , subq_7.metric_time__day - , subq_7.metric_time__week - , subq_7.metric_time__month - , subq_7.metric_time__quarter - , subq_7.metric_time__year - , subq_7.metric_time__extract_year - , subq_7.metric_time__extract_quarter - , subq_7.metric_time__extract_month - , subq_7.metric_time__extract_day - , subq_7.metric_time__extract_dow - , subq_7.metric_time__extract_doy - , subq_7.user - , subq_7.session - , subq_7.visit__user - , subq_7.visit__session - , subq_7.referrer_id - , subq_7.visit__referrer_id - , subq_7.visits - , subq_7.visitors + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.visit__ds__day + , subq_8.visit__ds__week + , subq_8.visit__ds__month + , subq_8.visit__ds__quarter + , subq_8.visit__ds__year + , subq_8.visit__ds__extract_year + , subq_8.visit__ds__extract_quarter + , subq_8.visit__ds__extract_month + , subq_8.visit__ds__extract_day + , subq_8.visit__ds__extract_dow + , subq_8.visit__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session + , subq_8.visit__user + , subq_8.visit__session + , subq_8.referrer_id + , subq_8.visit__referrer_id + , subq_8.visits + , subq_8.visitors FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] SELECT - subq_6.ds__day - , subq_6.ds__week - , subq_6.ds__month - , subq_6.ds__quarter - , subq_6.ds__year - , subq_6.ds__extract_year - , subq_6.ds__extract_quarter - , subq_6.ds__extract_month - , subq_6.ds__extract_day - , subq_6.ds__extract_dow - , subq_6.ds__extract_doy - , subq_6.visit__ds__day - , subq_6.visit__ds__week - , subq_6.visit__ds__month - , subq_6.visit__ds__quarter - , subq_6.visit__ds__year - , subq_6.visit__ds__extract_year - , subq_6.visit__ds__extract_quarter - , subq_6.visit__ds__extract_month - , subq_6.visit__ds__extract_day - , subq_6.visit__ds__extract_dow - , subq_6.visit__ds__extract_doy - , subq_6.ds__day AS metric_time__day - , subq_6.ds__week AS metric_time__week - , subq_6.ds__month AS metric_time__month - , subq_6.ds__quarter AS metric_time__quarter - , subq_6.ds__year AS metric_time__year - , subq_6.ds__extract_year AS metric_time__extract_year - , subq_6.ds__extract_quarter AS metric_time__extract_quarter - , subq_6.ds__extract_month AS metric_time__extract_month - , subq_6.ds__extract_day AS metric_time__extract_day - , subq_6.ds__extract_dow AS metric_time__extract_dow - , subq_6.ds__extract_doy AS metric_time__extract_doy - , subq_6.user - , subq_6.session - , subq_6.visit__user - , subq_6.visit__session - , subq_6.referrer_id - , subq_6.visit__referrer_id - , subq_6.visits - , subq_6.visitors + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_6 - ) subq_7 - WHERE subq_7.metric_time__day BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_8 - ) subq_9 + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE subq_7.metric_time__day BETWEEN '2020-01-01' AND '2020-01-02' + ) subq_8 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_9 + ) subq_10 INNER JOIN ( -- Add column with generated UUID SELECT - subq_11.ds__day - , subq_11.ds__week - , subq_11.ds__month - , subq_11.ds__quarter - , subq_11.ds__year - , subq_11.ds__extract_year - , subq_11.ds__extract_quarter - , subq_11.ds__extract_month - , subq_11.ds__extract_day - , subq_11.ds__extract_dow - , subq_11.ds__extract_doy - , subq_11.buy__ds__day - , subq_11.buy__ds__week - , subq_11.buy__ds__month - , subq_11.buy__ds__quarter - , subq_11.buy__ds__year - , subq_11.buy__ds__extract_year - , subq_11.buy__ds__extract_quarter - , subq_11.buy__ds__extract_month - , subq_11.buy__ds__extract_day - , subq_11.buy__ds__extract_dow - , subq_11.buy__ds__extract_doy - , subq_11.metric_time__day - , subq_11.metric_time__week - , subq_11.metric_time__month - , subq_11.metric_time__quarter - , subq_11.metric_time__year - , subq_11.metric_time__extract_year - , subq_11.metric_time__extract_quarter - , subq_11.metric_time__extract_month - , subq_11.metric_time__extract_day - , subq_11.metric_time__extract_dow - , subq_11.metric_time__extract_doy - , subq_11.user - , subq_11.session_id - , subq_11.buy__user - , subq_11.buy__session_id - , subq_11.buys - , subq_11.buyers + subq_12.ds__day + , subq_12.ds__week + , subq_12.ds__month + , subq_12.ds__quarter + , subq_12.ds__year + , subq_12.ds__extract_year + , subq_12.ds__extract_quarter + , subq_12.ds__extract_month + , subq_12.ds__extract_day + , subq_12.ds__extract_dow + , subq_12.ds__extract_doy + , subq_12.buy__ds__day + , subq_12.buy__ds__week + , subq_12.buy__ds__month + , subq_12.buy__ds__quarter + , subq_12.buy__ds__year + , subq_12.buy__ds__extract_year + , subq_12.buy__ds__extract_quarter + , subq_12.buy__ds__extract_month + , subq_12.buy__ds__extract_day + , subq_12.buy__ds__extract_dow + , subq_12.buy__ds__extract_doy + , subq_12.metric_time__day + , subq_12.metric_time__week + , subq_12.metric_time__month + , subq_12.metric_time__quarter + , subq_12.metric_time__year + , subq_12.metric_time__extract_year + , subq_12.metric_time__extract_quarter + , subq_12.metric_time__extract_month + , subq_12.metric_time__extract_day + , subq_12.metric_time__extract_dow + , subq_12.metric_time__extract_doy + , subq_12.user + , subq_12.session_id + , subq_12.buy__user + , subq_12.buy__session_id + , subq_12.buys + , subq_12.buyers , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_10.ds__day - , subq_10.ds__week - , subq_10.ds__month - , subq_10.ds__quarter - , subq_10.ds__year - , subq_10.ds__extract_year - , subq_10.ds__extract_quarter - , subq_10.ds__extract_month - , subq_10.ds__extract_day - , subq_10.ds__extract_dow - , subq_10.ds__extract_doy - , subq_10.buy__ds__day - , subq_10.buy__ds__week - , subq_10.buy__ds__month - , subq_10.buy__ds__quarter - , subq_10.buy__ds__year - , subq_10.buy__ds__extract_year - , subq_10.buy__ds__extract_quarter - , subq_10.buy__ds__extract_month - , subq_10.buy__ds__extract_day - , subq_10.buy__ds__extract_dow - , subq_10.buy__ds__extract_doy - , subq_10.ds__day AS metric_time__day - , subq_10.ds__week AS metric_time__week - , subq_10.ds__month AS metric_time__month - , subq_10.ds__quarter AS metric_time__quarter - , subq_10.ds__year AS metric_time__year - , subq_10.ds__extract_year AS metric_time__extract_year - , subq_10.ds__extract_quarter AS metric_time__extract_quarter - , subq_10.ds__extract_month AS metric_time__extract_month - , subq_10.ds__extract_day AS metric_time__extract_day - , subq_10.ds__extract_dow AS metric_time__extract_dow - , subq_10.ds__extract_doy AS metric_time__extract_doy - , subq_10.user - , subq_10.session_id - , subq_10.buy__user - , subq_10.buy__session_id - , subq_10.buys - , subq_10.buyers + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.ds__day AS metric_time__day + , subq_11.ds__week AS metric_time__week + , subq_11.ds__month AS metric_time__month + , subq_11.ds__quarter AS metric_time__quarter + , subq_11.ds__year AS metric_time__year + , subq_11.ds__extract_year AS metric_time__extract_year + , subq_11.ds__extract_quarter AS metric_time__extract_quarter + , subq_11.ds__extract_month AS metric_time__extract_month + , subq_11.ds__extract_day AS metric_time__extract_day + , subq_11.ds__extract_dow AS metric_time__extract_dow + , subq_11.ds__extract_doy AS metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -461,23 +507,23 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_10 - ) subq_11 - ) subq_12 + ) subq_11 + ) subq_12 + ) subq_13 ON ( - subq_9.user = subq_12.user + subq_10.user = subq_13.user ) AND ( - (subq_9.ds__day <= subq_12.ds__day) + (subq_10.ds__day <= subq_13.ds__day) ) - ) subq_13 - ) subq_14 - ) subq_15 + ) subq_14 + ) subq_15 + ) subq_16 GROUP BY - subq_15.visit__referrer_id - ) subq_16 + subq_16.visit__referrer_id + ) subq_17 ON - subq_5.visit__referrer_id = subq_16.visit__referrer_id + subq_5.visit__referrer_id = subq_17.visit__referrer_id GROUP BY - COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) -) subq_17 + COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) +) subq_18 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_time_constraint__plan0_optimized.sql index 1269c6a617..91a10b183c 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_time_constraint__plan0_optimized.sql @@ -5,9 +5,9 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) AS visit__referrer_id - , MAX(subq_23.visits) AS visits - , MAX(subq_34.buys) AS buys + COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) AS visit__referrer_id + , MAX(subq_24.visits) AS visits + , MAX(subq_36.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -24,11 +24,11 @@ FROM ( , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_21 + ) subq_22 WHERE visit__referrer_id = 'ref_id_01' GROUP BY visit__referrer_id - ) subq_23 + ) subq_24 FULL OUTER JOIN ( -- Find conversions for user within the range of INF -- Pass Only Elements: ['buys', 'visit__referrer_id'] @@ -39,53 +39,62 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_27.visits) OVER ( + FIRST_VALUE(subq_29.visits) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_27.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_29.visit__referrer_id) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_27.ds__day) OVER ( + , FIRST_VALUE(subq_29.ds__day) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_27.user) OVER ( + , FIRST_VALUE(subq_29.user) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_30.mf_internal_uuid AS mf_internal_uuid - , subq_30.buys AS buys + , subq_32.mf_internal_uuid AS mf_internal_uuid + , subq_32.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_27 + ds__day + , subq_27.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' + ) subq_27 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_29 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -96,19 +105,19 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_30 + ) subq_32 ON ( - subq_27.user = subq_30.user + subq_29.user = subq_32.user ) AND ( - (subq_27.ds__day <= subq_30.ds__day) + (subq_29.ds__day <= subq_32.ds__day) ) - ) subq_31 + ) subq_33 GROUP BY visit__referrer_id - ) subq_34 + ) subq_36 ON - subq_23.visit__referrer_id = subq_34.visit__referrer_id + subq_24.visit__referrer_id = subq_36.visit__referrer_id GROUP BY - COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) -) subq_35 + COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) +) subq_37 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_window__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_window__plan0.sql index f4fe4a5c1a..a7b355b340 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_window__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_window__plan0.sql @@ -1,13 +1,13 @@ -- Compute Metrics via Expressions SELECT - subq_15.metric_time__day - , CAST(subq_15.buys AS DOUBLE) / CAST(NULLIF(subq_15.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days + subq_16.metric_time__day + , CAST(subq_16.buys AS DOUBLE) / CAST(NULLIF(subq_16.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) AS metric_time__day + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) AS metric_time__day , MAX(subq_4.visits) AS visits - , MAX(subq_14.buys) AS buys + , MAX(subq_15.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -112,231 +112,277 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_13.metric_time__day - , SUM(subq_13.buys) AS buys + subq_14.metric_time__day + , SUM(subq_14.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'metric_time__day'] SELECT - subq_12.metric_time__day - , subq_12.buys + subq_13.metric_time__day + , subq_13.buys FROM ( -- Find conversions for user within the range of 7 day SELECT - subq_11.ds__day - , subq_11.metric_time__day - , subq_11.user - , subq_11.buys - , subq_11.visits + subq_12.ds__day + , subq_12.metric_time__day + , subq_12.user + , subq_12.buys + , subq_12.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_7.visits) OVER ( + FIRST_VALUE(subq_8.visits) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_7.ds__day) OVER ( + , FIRST_VALUE(subq_8.ds__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_7.metric_time__day) OVER ( + , FIRST_VALUE(subq_8.metric_time__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_7.user) OVER ( + , FIRST_VALUE(subq_8.user) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_10.mf_internal_uuid AS mf_internal_uuid - , subq_10.buys AS buys + , subq_11.mf_internal_uuid AS mf_internal_uuid + , subq_11.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_6.ds__day - , subq_6.metric_time__day - , subq_6.user - , subq_6.visits + subq_7.ds__day + , subq_7.metric_time__day + , subq_7.user + , subq_7.visits FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE SELECT - subq_5.ds__day - , subq_5.ds__week - , subq_5.ds__month - , subq_5.ds__quarter - , subq_5.ds__year - , subq_5.ds__extract_year - , subq_5.ds__extract_quarter - , subq_5.ds__extract_month - , subq_5.ds__extract_day - , subq_5.ds__extract_dow - , subq_5.ds__extract_doy - , subq_5.visit__ds__day - , subq_5.visit__ds__week - , subq_5.visit__ds__month - , subq_5.visit__ds__quarter - , subq_5.visit__ds__year - , subq_5.visit__ds__extract_year - , subq_5.visit__ds__extract_quarter - , subq_5.visit__ds__extract_month - , subq_5.visit__ds__extract_day - , subq_5.visit__ds__extract_dow - , subq_5.visit__ds__extract_doy - , subq_5.ds__day AS metric_time__day - , subq_5.ds__week AS metric_time__week - , subq_5.ds__month AS metric_time__month - , subq_5.ds__quarter AS metric_time__quarter - , subq_5.ds__year AS metric_time__year - , subq_5.ds__extract_year AS metric_time__extract_year - , subq_5.ds__extract_quarter AS metric_time__extract_quarter - , subq_5.ds__extract_month AS metric_time__extract_month - , subq_5.ds__extract_day AS metric_time__extract_day - , subq_5.ds__extract_dow AS metric_time__extract_dow - , subq_5.ds__extract_doy AS metric_time__extract_doy - , subq_5.user - , subq_5.session - , subq_5.visit__user - , subq_5.visit__session - , subq_5.referrer_id - , subq_5.visit__referrer_id - , subq_5.visits - , subq_5.visitors + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.metric_time__day + , subq_6.metric_time__week + , subq_6.metric_time__month + , subq_6.metric_time__quarter + , subq_6.metric_time__year + , subq_6.metric_time__extract_year + , subq_6.metric_time__extract_quarter + , subq_6.metric_time__extract_month + , subq_6.metric_time__extract_day + , subq_6.metric_time__extract_dow + , subq_6.metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_5 - ) subq_6 - ) subq_7 + subq_5.ds__day + , subq_5.ds__week + , subq_5.ds__month + , subq_5.ds__quarter + , subq_5.ds__year + , subq_5.ds__extract_year + , subq_5.ds__extract_quarter + , subq_5.ds__extract_month + , subq_5.ds__extract_day + , subq_5.ds__extract_dow + , subq_5.ds__extract_doy + , subq_5.visit__ds__day + , subq_5.visit__ds__week + , subq_5.visit__ds__month + , subq_5.visit__ds__quarter + , subq_5.visit__ds__year + , subq_5.visit__ds__extract_year + , subq_5.visit__ds__extract_quarter + , subq_5.visit__ds__extract_month + , subq_5.visit__ds__extract_day + , subq_5.visit__ds__extract_dow + , subq_5.visit__ds__extract_doy + , subq_5.ds__day AS metric_time__day + , subq_5.ds__week AS metric_time__week + , subq_5.ds__month AS metric_time__month + , subq_5.ds__quarter AS metric_time__quarter + , subq_5.ds__year AS metric_time__year + , subq_5.ds__extract_year AS metric_time__extract_year + , subq_5.ds__extract_quarter AS metric_time__extract_quarter + , subq_5.ds__extract_month AS metric_time__extract_month + , subq_5.ds__extract_day AS metric_time__extract_day + , subq_5.ds__extract_dow AS metric_time__extract_dow + , subq_5.ds__extract_doy AS metric_time__extract_doy + , subq_5.user + , subq_5.session + , subq_5.visit__user + , subq_5.visit__session + , subq_5.referrer_id + , subq_5.visit__referrer_id + , subq_5.visits + , subq_5.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_5 + ) subq_6 + WHERE metric_time__day = '2020-01-01' + ) subq_7 + ) subq_8 INNER JOIN ( -- Add column with generated UUID SELECT - subq_9.ds__day - , subq_9.ds__week - , subq_9.ds__month - , subq_9.ds__quarter - , subq_9.ds__year - , subq_9.ds__extract_year - , subq_9.ds__extract_quarter - , subq_9.ds__extract_month - , subq_9.ds__extract_day - , subq_9.ds__extract_dow - , subq_9.ds__extract_doy - , subq_9.buy__ds__day - , subq_9.buy__ds__week - , subq_9.buy__ds__month - , subq_9.buy__ds__quarter - , subq_9.buy__ds__year - , subq_9.buy__ds__extract_year - , subq_9.buy__ds__extract_quarter - , subq_9.buy__ds__extract_month - , subq_9.buy__ds__extract_day - , subq_9.buy__ds__extract_dow - , subq_9.buy__ds__extract_doy - , subq_9.metric_time__day - , subq_9.metric_time__week - , subq_9.metric_time__month - , subq_9.metric_time__quarter - , subq_9.metric_time__year - , subq_9.metric_time__extract_year - , subq_9.metric_time__extract_quarter - , subq_9.metric_time__extract_month - , subq_9.metric_time__extract_day - , subq_9.metric_time__extract_dow - , subq_9.metric_time__extract_doy - , subq_9.user - , subq_9.session_id - , subq_9.buy__user - , subq_9.buy__session_id - , subq_9.buys - , subq_9.buyers + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.metric_time__day + , subq_10.metric_time__week + , subq_10.metric_time__month + , subq_10.metric_time__quarter + , subq_10.metric_time__year + , subq_10.metric_time__extract_year + , subq_10.metric_time__extract_quarter + , subq_10.metric_time__extract_month + , subq_10.metric_time__extract_day + , subq_10.metric_time__extract_dow + , subq_10.metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_8.ds__day - , subq_8.ds__week - , subq_8.ds__month - , subq_8.ds__quarter - , subq_8.ds__year - , subq_8.ds__extract_year - , subq_8.ds__extract_quarter - , subq_8.ds__extract_month - , subq_8.ds__extract_day - , subq_8.ds__extract_dow - , subq_8.ds__extract_doy - , subq_8.buy__ds__day - , subq_8.buy__ds__week - , subq_8.buy__ds__month - , subq_8.buy__ds__quarter - , subq_8.buy__ds__year - , subq_8.buy__ds__extract_year - , subq_8.buy__ds__extract_quarter - , subq_8.buy__ds__extract_month - , subq_8.buy__ds__extract_day - , subq_8.buy__ds__extract_dow - , subq_8.buy__ds__extract_doy - , subq_8.ds__day AS metric_time__day - , subq_8.ds__week AS metric_time__week - , subq_8.ds__month AS metric_time__month - , subq_8.ds__quarter AS metric_time__quarter - , subq_8.ds__year AS metric_time__year - , subq_8.ds__extract_year AS metric_time__extract_year - , subq_8.ds__extract_quarter AS metric_time__extract_quarter - , subq_8.ds__extract_month AS metric_time__extract_month - , subq_8.ds__extract_day AS metric_time__extract_day - , subq_8.ds__extract_dow AS metric_time__extract_dow - , subq_8.ds__extract_doy AS metric_time__extract_doy - , subq_8.user - , subq_8.session_id - , subq_8.buy__user - , subq_8.buy__session_id - , subq_8.buys - , subq_8.buyers + subq_9.ds__day + , subq_9.ds__week + , subq_9.ds__month + , subq_9.ds__quarter + , subq_9.ds__year + , subq_9.ds__extract_year + , subq_9.ds__extract_quarter + , subq_9.ds__extract_month + , subq_9.ds__extract_day + , subq_9.ds__extract_dow + , subq_9.ds__extract_doy + , subq_9.buy__ds__day + , subq_9.buy__ds__week + , subq_9.buy__ds__month + , subq_9.buy__ds__quarter + , subq_9.buy__ds__year + , subq_9.buy__ds__extract_year + , subq_9.buy__ds__extract_quarter + , subq_9.buy__ds__extract_month + , subq_9.buy__ds__extract_day + , subq_9.buy__ds__extract_dow + , subq_9.buy__ds__extract_doy + , subq_9.ds__day AS metric_time__day + , subq_9.ds__week AS metric_time__week + , subq_9.ds__month AS metric_time__month + , subq_9.ds__quarter AS metric_time__quarter + , subq_9.ds__year AS metric_time__year + , subq_9.ds__extract_year AS metric_time__extract_year + , subq_9.ds__extract_quarter AS metric_time__extract_quarter + , subq_9.ds__extract_month AS metric_time__extract_month + , subq_9.ds__extract_day AS metric_time__extract_day + , subq_9.ds__extract_dow AS metric_time__extract_dow + , subq_9.ds__extract_doy AS metric_time__extract_doy + , subq_9.user + , subq_9.session_id + , subq_9.buy__user + , subq_9.buy__session_id + , subq_9.buys + , subq_9.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -369,27 +415,27 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_8 - ) subq_9 - ) subq_10 + ) subq_9 + ) subq_10 + ) subq_11 ON ( - subq_7.user = subq_10.user + subq_8.user = subq_11.user ) AND ( ( - subq_7.ds__day <= subq_10.ds__day + subq_8.ds__day <= subq_11.ds__day ) AND ( - subq_7.ds__day > subq_10.ds__day - INTERVAL 7 day + subq_8.ds__day > subq_11.ds__day - INTERVAL 7 day ) ) - ) subq_11 - ) subq_12 - ) subq_13 + ) subq_12 + ) subq_13 + ) subq_14 GROUP BY - subq_13.metric_time__day - ) subq_14 + subq_14.metric_time__day + ) subq_15 ON - subq_4.metric_time__day = subq_14.metric_time__day + subq_4.metric_time__day = subq_15.metric_time__day GROUP BY - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) -) subq_15 + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) +) subq_16 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_window__plan0_optimized.sql index 82f1fe235c..9d34b0a1dc 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_window__plan0_optimized.sql @@ -5,9 +5,9 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_20.visits) AS visits - , MAX(subq_30.buys) AS buys + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , MAX(subq_21.visits) AS visits + , MAX(subq_32.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -22,11 +22,11 @@ FROM ( DATE_TRUNC('day', ds) AS metric_time__day , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_18 + ) subq_19 WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__day - ) subq_20 + ) subq_21 FULL OUTER JOIN ( -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'metric_time__day'] @@ -37,51 +37,60 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_23.visits) OVER ( + FIRST_VALUE(subq_25.visits) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_23.ds__day) OVER ( + , FIRST_VALUE(subq_25.ds__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_23.metric_time__day) OVER ( + , FIRST_VALUE(subq_25.metric_time__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_23.user) OVER ( + , FIRST_VALUE(subq_25.user) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_26.mf_internal_uuid AS mf_internal_uuid - , subq_26.buys AS buys + , subq_28.mf_internal_uuid AS mf_internal_uuid + , subq_28.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + ds__day + , metric_time__day + , subq_23.user + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_23 + WHERE metric_time__day = '2020-01-01' + ) subq_25 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -92,23 +101,23 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_26 + ) subq_28 ON ( - subq_23.user = subq_26.user + subq_25.user = subq_28.user ) AND ( ( - subq_23.ds__day <= subq_26.ds__day + subq_25.ds__day <= subq_28.ds__day ) AND ( - subq_23.ds__day > subq_26.ds__day - INTERVAL 7 day + subq_25.ds__day > subq_28.ds__day - INTERVAL 7 day ) ) - ) subq_27 + ) subq_29 GROUP BY metric_time__day - ) subq_30 + ) subq_32 ON - subq_20.metric_time__day = subq_30.metric_time__day + subq_21.metric_time__day = subq_32.metric_time__day GROUP BY - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) -) subq_31 + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) +) subq_33 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_window_and_time_constraint__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_window_and_time_constraint__plan0.sql index d675fb6ad2..f4245bc4a3 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_window_and_time_constraint__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_window_and_time_constraint__plan0.sql @@ -1,15 +1,15 @@ -- Compute Metrics via Expressions SELECT - subq_17.metric_time__day - , subq_17.visit__referrer_id - , CAST(subq_17.buys AS DOUBLE) / CAST(NULLIF(subq_17.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days + subq_18.metric_time__day + , subq_18.visit__referrer_id + , CAST(subq_18.buys AS DOUBLE) / CAST(NULLIF(subq_18.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_5.metric_time__day, subq_16.metric_time__day) AS metric_time__day - , COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_5.metric_time__day, subq_17.metric_time__day) AS metric_time__day + , COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) AS visit__referrer_id , MAX(subq_5.visits) AS visits - , MAX(subq_16.buys) AS buys + , MAX(subq_17.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -164,289 +164,335 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_15.metric_time__day - , subq_15.visit__referrer_id - , SUM(subq_15.buys) AS buys + subq_16.metric_time__day + , subq_16.visit__referrer_id + , SUM(subq_16.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] SELECT - subq_14.metric_time__day - , subq_14.visit__referrer_id - , subq_14.buys + subq_15.metric_time__day + , subq_15.visit__referrer_id + , subq_15.buys FROM ( -- Find conversions for user within the range of 7 day SELECT - subq_13.ds__day - , subq_13.metric_time__day - , subq_13.user - , subq_13.visit__referrer_id - , subq_13.buys - , subq_13.visits + subq_14.ds__day + , subq_14.metric_time__day + , subq_14.user + , subq_14.visit__referrer_id + , subq_14.buys + , subq_14.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_9.visits) OVER ( + FIRST_VALUE(subq_10.visits) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_9.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_10.visit__referrer_id) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_9.ds__day) OVER ( + , FIRST_VALUE(subq_10.ds__day) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_9.metric_time__day) OVER ( + , FIRST_VALUE(subq_10.metric_time__day) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_9.user) OVER ( + , FIRST_VALUE(subq_10.user) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_12.mf_internal_uuid AS mf_internal_uuid - , subq_12.buys AS buys + , subq_13.mf_internal_uuid AS mf_internal_uuid + , subq_13.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_8.ds__day - , subq_8.metric_time__day - , subq_8.user - , subq_8.visit__referrer_id - , subq_8.visits + subq_9.ds__day + , subq_9.metric_time__day + , subq_9.user + , subq_9.visit__referrer_id + , subq_9.visits FROM ( - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE SELECT - subq_7.ds__day - , subq_7.ds__week - , subq_7.ds__month - , subq_7.ds__quarter - , subq_7.ds__year - , subq_7.ds__extract_year - , subq_7.ds__extract_quarter - , subq_7.ds__extract_month - , subq_7.ds__extract_day - , subq_7.ds__extract_dow - , subq_7.ds__extract_doy - , subq_7.visit__ds__day - , subq_7.visit__ds__week - , subq_7.visit__ds__month - , subq_7.visit__ds__quarter - , subq_7.visit__ds__year - , subq_7.visit__ds__extract_year - , subq_7.visit__ds__extract_quarter - , subq_7.visit__ds__extract_month - , subq_7.visit__ds__extract_day - , subq_7.visit__ds__extract_dow - , subq_7.visit__ds__extract_doy - , subq_7.metric_time__day - , subq_7.metric_time__week - , subq_7.metric_time__month - , subq_7.metric_time__quarter - , subq_7.metric_time__year - , subq_7.metric_time__extract_year - , subq_7.metric_time__extract_quarter - , subq_7.metric_time__extract_month - , subq_7.metric_time__extract_day - , subq_7.metric_time__extract_dow - , subq_7.metric_time__extract_doy - , subq_7.user - , subq_7.session - , subq_7.visit__user - , subq_7.visit__session - , subq_7.referrer_id - , subq_7.visit__referrer_id - , subq_7.visits - , subq_7.visitors + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.visit__ds__day + , subq_8.visit__ds__week + , subq_8.visit__ds__month + , subq_8.visit__ds__quarter + , subq_8.visit__ds__year + , subq_8.visit__ds__extract_year + , subq_8.visit__ds__extract_quarter + , subq_8.visit__ds__extract_month + , subq_8.visit__ds__extract_day + , subq_8.visit__ds__extract_dow + , subq_8.visit__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session + , subq_8.visit__user + , subq_8.visit__session + , subq_8.referrer_id + , subq_8.visit__referrer_id + , subq_8.visits + , subq_8.visitors FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] SELECT - subq_6.ds__day - , subq_6.ds__week - , subq_6.ds__month - , subq_6.ds__quarter - , subq_6.ds__year - , subq_6.ds__extract_year - , subq_6.ds__extract_quarter - , subq_6.ds__extract_month - , subq_6.ds__extract_day - , subq_6.ds__extract_dow - , subq_6.ds__extract_doy - , subq_6.visit__ds__day - , subq_6.visit__ds__week - , subq_6.visit__ds__month - , subq_6.visit__ds__quarter - , subq_6.visit__ds__year - , subq_6.visit__ds__extract_year - , subq_6.visit__ds__extract_quarter - , subq_6.visit__ds__extract_month - , subq_6.visit__ds__extract_day - , subq_6.visit__ds__extract_dow - , subq_6.visit__ds__extract_doy - , subq_6.ds__day AS metric_time__day - , subq_6.ds__week AS metric_time__week - , subq_6.ds__month AS metric_time__month - , subq_6.ds__quarter AS metric_time__quarter - , subq_6.ds__year AS metric_time__year - , subq_6.ds__extract_year AS metric_time__extract_year - , subq_6.ds__extract_quarter AS metric_time__extract_quarter - , subq_6.ds__extract_month AS metric_time__extract_month - , subq_6.ds__extract_day AS metric_time__extract_day - , subq_6.ds__extract_dow AS metric_time__extract_dow - , subq_6.ds__extract_doy AS metric_time__extract_doy - , subq_6.user - , subq_6.session - , subq_6.visit__user - , subq_6.visit__session - , subq_6.referrer_id - , subq_6.visit__referrer_id - , subq_6.visits - , subq_6.visitors + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_6 - ) subq_7 - WHERE subq_7.metric_time__day BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_8 - ) subq_9 + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE subq_7.metric_time__day BETWEEN '2020-01-01' AND '2020-01-02' + ) subq_8 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_9 + ) subq_10 INNER JOIN ( -- Add column with generated UUID SELECT - subq_11.ds__day - , subq_11.ds__week - , subq_11.ds__month - , subq_11.ds__quarter - , subq_11.ds__year - , subq_11.ds__extract_year - , subq_11.ds__extract_quarter - , subq_11.ds__extract_month - , subq_11.ds__extract_day - , subq_11.ds__extract_dow - , subq_11.ds__extract_doy - , subq_11.buy__ds__day - , subq_11.buy__ds__week - , subq_11.buy__ds__month - , subq_11.buy__ds__quarter - , subq_11.buy__ds__year - , subq_11.buy__ds__extract_year - , subq_11.buy__ds__extract_quarter - , subq_11.buy__ds__extract_month - , subq_11.buy__ds__extract_day - , subq_11.buy__ds__extract_dow - , subq_11.buy__ds__extract_doy - , subq_11.metric_time__day - , subq_11.metric_time__week - , subq_11.metric_time__month - , subq_11.metric_time__quarter - , subq_11.metric_time__year - , subq_11.metric_time__extract_year - , subq_11.metric_time__extract_quarter - , subq_11.metric_time__extract_month - , subq_11.metric_time__extract_day - , subq_11.metric_time__extract_dow - , subq_11.metric_time__extract_doy - , subq_11.user - , subq_11.session_id - , subq_11.buy__user - , subq_11.buy__session_id - , subq_11.buys - , subq_11.buyers + subq_12.ds__day + , subq_12.ds__week + , subq_12.ds__month + , subq_12.ds__quarter + , subq_12.ds__year + , subq_12.ds__extract_year + , subq_12.ds__extract_quarter + , subq_12.ds__extract_month + , subq_12.ds__extract_day + , subq_12.ds__extract_dow + , subq_12.ds__extract_doy + , subq_12.buy__ds__day + , subq_12.buy__ds__week + , subq_12.buy__ds__month + , subq_12.buy__ds__quarter + , subq_12.buy__ds__year + , subq_12.buy__ds__extract_year + , subq_12.buy__ds__extract_quarter + , subq_12.buy__ds__extract_month + , subq_12.buy__ds__extract_day + , subq_12.buy__ds__extract_dow + , subq_12.buy__ds__extract_doy + , subq_12.metric_time__day + , subq_12.metric_time__week + , subq_12.metric_time__month + , subq_12.metric_time__quarter + , subq_12.metric_time__year + , subq_12.metric_time__extract_year + , subq_12.metric_time__extract_quarter + , subq_12.metric_time__extract_month + , subq_12.metric_time__extract_day + , subq_12.metric_time__extract_dow + , subq_12.metric_time__extract_doy + , subq_12.user + , subq_12.session_id + , subq_12.buy__user + , subq_12.buy__session_id + , subq_12.buys + , subq_12.buyers , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_10.ds__day - , subq_10.ds__week - , subq_10.ds__month - , subq_10.ds__quarter - , subq_10.ds__year - , subq_10.ds__extract_year - , subq_10.ds__extract_quarter - , subq_10.ds__extract_month - , subq_10.ds__extract_day - , subq_10.ds__extract_dow - , subq_10.ds__extract_doy - , subq_10.buy__ds__day - , subq_10.buy__ds__week - , subq_10.buy__ds__month - , subq_10.buy__ds__quarter - , subq_10.buy__ds__year - , subq_10.buy__ds__extract_year - , subq_10.buy__ds__extract_quarter - , subq_10.buy__ds__extract_month - , subq_10.buy__ds__extract_day - , subq_10.buy__ds__extract_dow - , subq_10.buy__ds__extract_doy - , subq_10.ds__day AS metric_time__day - , subq_10.ds__week AS metric_time__week - , subq_10.ds__month AS metric_time__month - , subq_10.ds__quarter AS metric_time__quarter - , subq_10.ds__year AS metric_time__year - , subq_10.ds__extract_year AS metric_time__extract_year - , subq_10.ds__extract_quarter AS metric_time__extract_quarter - , subq_10.ds__extract_month AS metric_time__extract_month - , subq_10.ds__extract_day AS metric_time__extract_day - , subq_10.ds__extract_dow AS metric_time__extract_dow - , subq_10.ds__extract_doy AS metric_time__extract_doy - , subq_10.user - , subq_10.session_id - , subq_10.buy__user - , subq_10.buy__session_id - , subq_10.buys - , subq_10.buyers + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.ds__day AS metric_time__day + , subq_11.ds__week AS metric_time__week + , subq_11.ds__month AS metric_time__month + , subq_11.ds__quarter AS metric_time__quarter + , subq_11.ds__year AS metric_time__year + , subq_11.ds__extract_year AS metric_time__extract_year + , subq_11.ds__extract_quarter AS metric_time__extract_quarter + , subq_11.ds__extract_month AS metric_time__extract_month + , subq_11.ds__extract_day AS metric_time__extract_day + , subq_11.ds__extract_dow AS metric_time__extract_dow + , subq_11.ds__extract_doy AS metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -479,33 +525,33 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_10 - ) subq_11 - ) subq_12 + ) subq_11 + ) subq_12 + ) subq_13 ON ( - subq_9.user = subq_12.user + subq_10.user = subq_13.user ) AND ( ( - subq_9.ds__day <= subq_12.ds__day + subq_10.ds__day <= subq_13.ds__day ) AND ( - subq_9.ds__day > subq_12.ds__day - INTERVAL 7 day + subq_10.ds__day > subq_13.ds__day - INTERVAL 7 day ) ) - ) subq_13 - ) subq_14 - ) subq_15 + ) subq_14 + ) subq_15 + ) subq_16 GROUP BY - subq_15.metric_time__day - , subq_15.visit__referrer_id - ) subq_16 + subq_16.metric_time__day + , subq_16.visit__referrer_id + ) subq_17 ON ( - subq_5.visit__referrer_id = subq_16.visit__referrer_id + subq_5.visit__referrer_id = subq_17.visit__referrer_id ) AND ( - subq_5.metric_time__day = subq_16.metric_time__day + subq_5.metric_time__day = subq_17.metric_time__day ) GROUP BY - COALESCE(subq_5.metric_time__day, subq_16.metric_time__day) - , COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) -) subq_17 + COALESCE(subq_5.metric_time__day, subq_17.metric_time__day) + , COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) +) subq_18 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql index 121a8a2629..84ef0b8132 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql @@ -6,10 +6,10 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_23.metric_time__day, subq_34.metric_time__day) AS metric_time__day - , COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) AS visit__referrer_id - , MAX(subq_23.visits) AS visits - , MAX(subq_34.buys) AS buys + COALESCE(subq_24.metric_time__day, subq_36.metric_time__day) AS metric_time__day + , COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) AS visit__referrer_id + , MAX(subq_24.visits) AS visits + , MAX(subq_36.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -28,12 +28,12 @@ FROM ( , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_21 + ) subq_22 WHERE visit__referrer_id = 'ref_id_01' GROUP BY metric_time__day , visit__referrer_id - ) subq_23 + ) subq_24 FULL OUTER JOIN ( -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] @@ -45,62 +45,72 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_27.visits) OVER ( + FIRST_VALUE(subq_29.visits) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_27.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_29.visit__referrer_id) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_27.ds__day) OVER ( + , FIRST_VALUE(subq_29.ds__day) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_27.metric_time__day) OVER ( + , FIRST_VALUE(subq_29.metric_time__day) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_27.user) OVER ( + , FIRST_VALUE(subq_29.user) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_30.mf_internal_uuid AS mf_internal_uuid - , subq_30.buys AS buys + , subq_32.mf_internal_uuid AS mf_internal_uuid + , subq_32.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_27 + ds__day + , metric_time__day + , subq_27.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' + ) subq_27 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_29 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -111,29 +121,29 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_30 + ) subq_32 ON ( - subq_27.user = subq_30.user + subq_29.user = subq_32.user ) AND ( ( - subq_27.ds__day <= subq_30.ds__day + subq_29.ds__day <= subq_32.ds__day ) AND ( - subq_27.ds__day > subq_30.ds__day - INTERVAL 7 day + subq_29.ds__day > subq_32.ds__day - INTERVAL 7 day ) ) - ) subq_31 + ) subq_33 GROUP BY metric_time__day , visit__referrer_id - ) subq_34 + ) subq_36 ON ( - subq_23.visit__referrer_id = subq_34.visit__referrer_id + subq_24.visit__referrer_id = subq_36.visit__referrer_id ) AND ( - subq_23.metric_time__day = subq_34.metric_time__day + subq_24.metric_time__day = subq_36.metric_time__day ) GROUP BY - COALESCE(subq_23.metric_time__day, subq_34.metric_time__day) - , COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) -) subq_35 + COALESCE(subq_24.metric_time__day, subq_36.metric_time__day) + , COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) +) subq_37 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric__plan0.sql index 45bcc25f40..acc4a4eec0 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric__plan0.sql @@ -1,13 +1,13 @@ -- Compute Metrics via Expressions SELECT - subq_15.metric_time__day - , CAST(subq_15.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_15.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate + subq_16.metric_time__day + , CAST(subq_16.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_16.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) AS metric_time__day + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) AS metric_time__day , MAX(subq_4.visits) AS visits - , MAX(subq_14.buys) AS buys + , MAX(subq_15.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -112,231 +112,277 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_13.metric_time__day - , SUM(subq_13.buys) AS buys + subq_14.metric_time__day + , SUM(subq_14.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'metric_time__day'] SELECT - subq_12.metric_time__day - , subq_12.buys + subq_13.metric_time__day + , subq_13.buys FROM ( -- Find conversions for user within the range of INF SELECT - subq_11.ds__day - , subq_11.metric_time__day - , subq_11.user - , subq_11.buys - , subq_11.visits + subq_12.ds__day + , subq_12.metric_time__day + , subq_12.user + , subq_12.buys + , subq_12.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_7.visits) OVER ( + FIRST_VALUE(subq_8.visits) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_7.ds__day) OVER ( + , FIRST_VALUE(subq_8.ds__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_7.metric_time__day) OVER ( + , FIRST_VALUE(subq_8.metric_time__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_7.user) OVER ( + , FIRST_VALUE(subq_8.user) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_10.mf_internal_uuid AS mf_internal_uuid - , subq_10.buys AS buys + , subq_11.mf_internal_uuid AS mf_internal_uuid + , subq_11.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_6.ds__day - , subq_6.metric_time__day - , subq_6.user - , subq_6.visits + subq_7.ds__day + , subq_7.metric_time__day + , subq_7.user + , subq_7.visits FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE SELECT - subq_5.ds__day - , subq_5.ds__week - , subq_5.ds__month - , subq_5.ds__quarter - , subq_5.ds__year - , subq_5.ds__extract_year - , subq_5.ds__extract_quarter - , subq_5.ds__extract_month - , subq_5.ds__extract_day - , subq_5.ds__extract_dow - , subq_5.ds__extract_doy - , subq_5.visit__ds__day - , subq_5.visit__ds__week - , subq_5.visit__ds__month - , subq_5.visit__ds__quarter - , subq_5.visit__ds__year - , subq_5.visit__ds__extract_year - , subq_5.visit__ds__extract_quarter - , subq_5.visit__ds__extract_month - , subq_5.visit__ds__extract_day - , subq_5.visit__ds__extract_dow - , subq_5.visit__ds__extract_doy - , subq_5.ds__day AS metric_time__day - , subq_5.ds__week AS metric_time__week - , subq_5.ds__month AS metric_time__month - , subq_5.ds__quarter AS metric_time__quarter - , subq_5.ds__year AS metric_time__year - , subq_5.ds__extract_year AS metric_time__extract_year - , subq_5.ds__extract_quarter AS metric_time__extract_quarter - , subq_5.ds__extract_month AS metric_time__extract_month - , subq_5.ds__extract_day AS metric_time__extract_day - , subq_5.ds__extract_dow AS metric_time__extract_dow - , subq_5.ds__extract_doy AS metric_time__extract_doy - , subq_5.user - , subq_5.session - , subq_5.visit__user - , subq_5.visit__session - , subq_5.referrer_id - , subq_5.visit__referrer_id - , subq_5.visits - , subq_5.visitors + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.metric_time__day + , subq_6.metric_time__week + , subq_6.metric_time__month + , subq_6.metric_time__quarter + , subq_6.metric_time__year + , subq_6.metric_time__extract_year + , subq_6.metric_time__extract_quarter + , subq_6.metric_time__extract_month + , subq_6.metric_time__extract_day + , subq_6.metric_time__extract_dow + , subq_6.metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_5 - ) subq_6 - ) subq_7 + subq_5.ds__day + , subq_5.ds__week + , subq_5.ds__month + , subq_5.ds__quarter + , subq_5.ds__year + , subq_5.ds__extract_year + , subq_5.ds__extract_quarter + , subq_5.ds__extract_month + , subq_5.ds__extract_day + , subq_5.ds__extract_dow + , subq_5.ds__extract_doy + , subq_5.visit__ds__day + , subq_5.visit__ds__week + , subq_5.visit__ds__month + , subq_5.visit__ds__quarter + , subq_5.visit__ds__year + , subq_5.visit__ds__extract_year + , subq_5.visit__ds__extract_quarter + , subq_5.visit__ds__extract_month + , subq_5.visit__ds__extract_day + , subq_5.visit__ds__extract_dow + , subq_5.visit__ds__extract_doy + , subq_5.ds__day AS metric_time__day + , subq_5.ds__week AS metric_time__week + , subq_5.ds__month AS metric_time__month + , subq_5.ds__quarter AS metric_time__quarter + , subq_5.ds__year AS metric_time__year + , subq_5.ds__extract_year AS metric_time__extract_year + , subq_5.ds__extract_quarter AS metric_time__extract_quarter + , subq_5.ds__extract_month AS metric_time__extract_month + , subq_5.ds__extract_day AS metric_time__extract_day + , subq_5.ds__extract_dow AS metric_time__extract_dow + , subq_5.ds__extract_doy AS metric_time__extract_doy + , subq_5.user + , subq_5.session + , subq_5.visit__user + , subq_5.visit__session + , subq_5.referrer_id + , subq_5.visit__referrer_id + , subq_5.visits + , subq_5.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_5 + ) subq_6 + WHERE metric_time__day = '2020-01-01' + ) subq_7 + ) subq_8 INNER JOIN ( -- Add column with generated UUID SELECT - subq_9.ds__day - , subq_9.ds__week - , subq_9.ds__month - , subq_9.ds__quarter - , subq_9.ds__year - , subq_9.ds__extract_year - , subq_9.ds__extract_quarter - , subq_9.ds__extract_month - , subq_9.ds__extract_day - , subq_9.ds__extract_dow - , subq_9.ds__extract_doy - , subq_9.buy__ds__day - , subq_9.buy__ds__week - , subq_9.buy__ds__month - , subq_9.buy__ds__quarter - , subq_9.buy__ds__year - , subq_9.buy__ds__extract_year - , subq_9.buy__ds__extract_quarter - , subq_9.buy__ds__extract_month - , subq_9.buy__ds__extract_day - , subq_9.buy__ds__extract_dow - , subq_9.buy__ds__extract_doy - , subq_9.metric_time__day - , subq_9.metric_time__week - , subq_9.metric_time__month - , subq_9.metric_time__quarter - , subq_9.metric_time__year - , subq_9.metric_time__extract_year - , subq_9.metric_time__extract_quarter - , subq_9.metric_time__extract_month - , subq_9.metric_time__extract_day - , subq_9.metric_time__extract_dow - , subq_9.metric_time__extract_doy - , subq_9.user - , subq_9.session_id - , subq_9.buy__user - , subq_9.buy__session_id - , subq_9.buys - , subq_9.buyers + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.metric_time__day + , subq_10.metric_time__week + , subq_10.metric_time__month + , subq_10.metric_time__quarter + , subq_10.metric_time__year + , subq_10.metric_time__extract_year + , subq_10.metric_time__extract_quarter + , subq_10.metric_time__extract_month + , subq_10.metric_time__extract_day + , subq_10.metric_time__extract_dow + , subq_10.metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_8.ds__day - , subq_8.ds__week - , subq_8.ds__month - , subq_8.ds__quarter - , subq_8.ds__year - , subq_8.ds__extract_year - , subq_8.ds__extract_quarter - , subq_8.ds__extract_month - , subq_8.ds__extract_day - , subq_8.ds__extract_dow - , subq_8.ds__extract_doy - , subq_8.buy__ds__day - , subq_8.buy__ds__week - , subq_8.buy__ds__month - , subq_8.buy__ds__quarter - , subq_8.buy__ds__year - , subq_8.buy__ds__extract_year - , subq_8.buy__ds__extract_quarter - , subq_8.buy__ds__extract_month - , subq_8.buy__ds__extract_day - , subq_8.buy__ds__extract_dow - , subq_8.buy__ds__extract_doy - , subq_8.ds__day AS metric_time__day - , subq_8.ds__week AS metric_time__week - , subq_8.ds__month AS metric_time__month - , subq_8.ds__quarter AS metric_time__quarter - , subq_8.ds__year AS metric_time__year - , subq_8.ds__extract_year AS metric_time__extract_year - , subq_8.ds__extract_quarter AS metric_time__extract_quarter - , subq_8.ds__extract_month AS metric_time__extract_month - , subq_8.ds__extract_day AS metric_time__extract_day - , subq_8.ds__extract_dow AS metric_time__extract_dow - , subq_8.ds__extract_doy AS metric_time__extract_doy - , subq_8.user - , subq_8.session_id - , subq_8.buy__user - , subq_8.buy__session_id - , subq_8.buys - , subq_8.buyers + subq_9.ds__day + , subq_9.ds__week + , subq_9.ds__month + , subq_9.ds__quarter + , subq_9.ds__year + , subq_9.ds__extract_year + , subq_9.ds__extract_quarter + , subq_9.ds__extract_month + , subq_9.ds__extract_day + , subq_9.ds__extract_dow + , subq_9.ds__extract_doy + , subq_9.buy__ds__day + , subq_9.buy__ds__week + , subq_9.buy__ds__month + , subq_9.buy__ds__quarter + , subq_9.buy__ds__year + , subq_9.buy__ds__extract_year + , subq_9.buy__ds__extract_quarter + , subq_9.buy__ds__extract_month + , subq_9.buy__ds__extract_day + , subq_9.buy__ds__extract_dow + , subq_9.buy__ds__extract_doy + , subq_9.ds__day AS metric_time__day + , subq_9.ds__week AS metric_time__week + , subq_9.ds__month AS metric_time__month + , subq_9.ds__quarter AS metric_time__quarter + , subq_9.ds__year AS metric_time__year + , subq_9.ds__extract_year AS metric_time__extract_year + , subq_9.ds__extract_quarter AS metric_time__extract_quarter + , subq_9.ds__extract_month AS metric_time__extract_month + , subq_9.ds__extract_day AS metric_time__extract_day + , subq_9.ds__extract_dow AS metric_time__extract_dow + , subq_9.ds__extract_doy AS metric_time__extract_doy + , subq_9.user + , subq_9.session_id + , subq_9.buy__user + , subq_9.buy__session_id + , subq_9.buys + , subq_9.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -369,23 +415,23 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_8 - ) subq_9 - ) subq_10 + ) subq_9 + ) subq_10 + ) subq_11 ON ( - subq_7.user = subq_10.user + subq_8.user = subq_11.user ) AND ( - (subq_7.ds__day <= subq_10.ds__day) + (subq_8.ds__day <= subq_11.ds__day) ) - ) subq_11 - ) subq_12 - ) subq_13 + ) subq_12 + ) subq_13 + ) subq_14 GROUP BY - subq_13.metric_time__day - ) subq_14 + subq_14.metric_time__day + ) subq_15 ON - subq_4.metric_time__day = subq_14.metric_time__day + subq_4.metric_time__day = subq_15.metric_time__day GROUP BY - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) -) subq_15 + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) +) subq_16 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric__plan0_optimized.sql index f729a92ce9..b371ce692c 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric__plan0_optimized.sql @@ -5,9 +5,9 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_20.visits) AS visits - , MAX(subq_30.buys) AS buys + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , MAX(subq_21.visits) AS visits + , MAX(subq_32.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -22,11 +22,11 @@ FROM ( DATE_TRUNC('day', ds) AS metric_time__day , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_18 + ) subq_19 WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__day - ) subq_20 + ) subq_21 FULL OUTER JOIN ( -- Find conversions for user within the range of INF -- Pass Only Elements: ['buys', 'metric_time__day'] @@ -37,51 +37,60 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_23.visits) OVER ( + FIRST_VALUE(subq_25.visits) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_23.ds__day) OVER ( + , FIRST_VALUE(subq_25.ds__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_23.metric_time__day) OVER ( + , FIRST_VALUE(subq_25.metric_time__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_23.user) OVER ( + , FIRST_VALUE(subq_25.user) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_26.mf_internal_uuid AS mf_internal_uuid - , subq_26.buys AS buys + , subq_28.mf_internal_uuid AS mf_internal_uuid + , subq_28.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + ds__day + , metric_time__day + , subq_23.user + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_23 + WHERE metric_time__day = '2020-01-01' + ) subq_25 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -92,19 +101,19 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_26 + ) subq_28 ON ( - subq_23.user = subq_26.user + subq_25.user = subq_28.user ) AND ( - (subq_23.ds__day <= subq_26.ds__day) + (subq_25.ds__day <= subq_28.ds__day) ) - ) subq_27 + ) subq_29 GROUP BY metric_time__day - ) subq_30 + ) subq_32 ON - subq_20.metric_time__day = subq_30.metric_time__day + subq_21.metric_time__day = subq_32.metric_time__day GROUP BY - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) -) subq_31 + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) +) subq_33 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_categorical_filter__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_categorical_filter__plan0.sql index 7511fe9e6c..999db36ceb 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_categorical_filter__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_categorical_filter__plan0.sql @@ -1,15 +1,15 @@ -- Compute Metrics via Expressions SELECT - subq_15.metric_time__day - , subq_15.visit__referrer_id - , CAST(subq_15.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_15.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate + subq_16.metric_time__day + , subq_16.visit__referrer_id + , CAST(subq_16.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_16.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) AS metric_time__day - , COALESCE(subq_4.visit__referrer_id, subq_14.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) AS metric_time__day + , COALESCE(subq_4.visit__referrer_id, subq_15.visit__referrer_id) AS visit__referrer_id , MAX(subq_4.visits) AS visits - , MAX(subq_14.buys) AS buys + , MAX(subq_15.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -118,243 +118,289 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_13.metric_time__day - , subq_13.visit__referrer_id - , SUM(subq_13.buys) AS buys + subq_14.metric_time__day + , subq_14.visit__referrer_id + , SUM(subq_14.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] SELECT - subq_12.metric_time__day - , subq_12.visit__referrer_id - , subq_12.buys + subq_13.metric_time__day + , subq_13.visit__referrer_id + , subq_13.buys FROM ( -- Find conversions for user within the range of INF SELECT - subq_11.ds__day - , subq_11.metric_time__day - , subq_11.user - , subq_11.visit__referrer_id - , subq_11.buys - , subq_11.visits + subq_12.ds__day + , subq_12.metric_time__day + , subq_12.user + , subq_12.visit__referrer_id + , subq_12.buys + , subq_12.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_7.visits) OVER ( + FIRST_VALUE(subq_8.visits) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_7.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_8.visit__referrer_id) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_7.ds__day) OVER ( + , FIRST_VALUE(subq_8.ds__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_7.metric_time__day) OVER ( + , FIRST_VALUE(subq_8.metric_time__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_7.user) OVER ( + , FIRST_VALUE(subq_8.user) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_10.mf_internal_uuid AS mf_internal_uuid - , subq_10.buys AS buys + , subq_11.mf_internal_uuid AS mf_internal_uuid + , subq_11.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_6.ds__day - , subq_6.metric_time__day - , subq_6.user - , subq_6.visit__referrer_id - , subq_6.visits + subq_7.ds__day + , subq_7.metric_time__day + , subq_7.user + , subq_7.visit__referrer_id + , subq_7.visits FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE SELECT - subq_5.ds__day - , subq_5.ds__week - , subq_5.ds__month - , subq_5.ds__quarter - , subq_5.ds__year - , subq_5.ds__extract_year - , subq_5.ds__extract_quarter - , subq_5.ds__extract_month - , subq_5.ds__extract_day - , subq_5.ds__extract_dow - , subq_5.ds__extract_doy - , subq_5.visit__ds__day - , subq_5.visit__ds__week - , subq_5.visit__ds__month - , subq_5.visit__ds__quarter - , subq_5.visit__ds__year - , subq_5.visit__ds__extract_year - , subq_5.visit__ds__extract_quarter - , subq_5.visit__ds__extract_month - , subq_5.visit__ds__extract_day - , subq_5.visit__ds__extract_dow - , subq_5.visit__ds__extract_doy - , subq_5.ds__day AS metric_time__day - , subq_5.ds__week AS metric_time__week - , subq_5.ds__month AS metric_time__month - , subq_5.ds__quarter AS metric_time__quarter - , subq_5.ds__year AS metric_time__year - , subq_5.ds__extract_year AS metric_time__extract_year - , subq_5.ds__extract_quarter AS metric_time__extract_quarter - , subq_5.ds__extract_month AS metric_time__extract_month - , subq_5.ds__extract_day AS metric_time__extract_day - , subq_5.ds__extract_dow AS metric_time__extract_dow - , subq_5.ds__extract_doy AS metric_time__extract_doy - , subq_5.user - , subq_5.session - , subq_5.visit__user - , subq_5.visit__session - , subq_5.referrer_id - , subq_5.visit__referrer_id - , subq_5.visits - , subq_5.visitors + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.metric_time__day + , subq_6.metric_time__week + , subq_6.metric_time__month + , subq_6.metric_time__quarter + , subq_6.metric_time__year + , subq_6.metric_time__extract_year + , subq_6.metric_time__extract_quarter + , subq_6.metric_time__extract_month + , subq_6.metric_time__extract_day + , subq_6.metric_time__extract_dow + , subq_6.metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_5 - ) subq_6 - ) subq_7 + subq_5.ds__day + , subq_5.ds__week + , subq_5.ds__month + , subq_5.ds__quarter + , subq_5.ds__year + , subq_5.ds__extract_year + , subq_5.ds__extract_quarter + , subq_5.ds__extract_month + , subq_5.ds__extract_day + , subq_5.ds__extract_dow + , subq_5.ds__extract_doy + , subq_5.visit__ds__day + , subq_5.visit__ds__week + , subq_5.visit__ds__month + , subq_5.visit__ds__quarter + , subq_5.visit__ds__year + , subq_5.visit__ds__extract_year + , subq_5.visit__ds__extract_quarter + , subq_5.visit__ds__extract_month + , subq_5.visit__ds__extract_day + , subq_5.visit__ds__extract_dow + , subq_5.visit__ds__extract_doy + , subq_5.ds__day AS metric_time__day + , subq_5.ds__week AS metric_time__week + , subq_5.ds__month AS metric_time__month + , subq_5.ds__quarter AS metric_time__quarter + , subq_5.ds__year AS metric_time__year + , subq_5.ds__extract_year AS metric_time__extract_year + , subq_5.ds__extract_quarter AS metric_time__extract_quarter + , subq_5.ds__extract_month AS metric_time__extract_month + , subq_5.ds__extract_day AS metric_time__extract_day + , subq_5.ds__extract_dow AS metric_time__extract_dow + , subq_5.ds__extract_doy AS metric_time__extract_doy + , subq_5.user + , subq_5.session + , subq_5.visit__user + , subq_5.visit__session + , subq_5.referrer_id + , subq_5.visit__referrer_id + , subq_5.visits + , subq_5.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_5 + ) subq_6 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_7 + ) subq_8 INNER JOIN ( -- Add column with generated UUID SELECT - subq_9.ds__day - , subq_9.ds__week - , subq_9.ds__month - , subq_9.ds__quarter - , subq_9.ds__year - , subq_9.ds__extract_year - , subq_9.ds__extract_quarter - , subq_9.ds__extract_month - , subq_9.ds__extract_day - , subq_9.ds__extract_dow - , subq_9.ds__extract_doy - , subq_9.buy__ds__day - , subq_9.buy__ds__week - , subq_9.buy__ds__month - , subq_9.buy__ds__quarter - , subq_9.buy__ds__year - , subq_9.buy__ds__extract_year - , subq_9.buy__ds__extract_quarter - , subq_9.buy__ds__extract_month - , subq_9.buy__ds__extract_day - , subq_9.buy__ds__extract_dow - , subq_9.buy__ds__extract_doy - , subq_9.metric_time__day - , subq_9.metric_time__week - , subq_9.metric_time__month - , subq_9.metric_time__quarter - , subq_9.metric_time__year - , subq_9.metric_time__extract_year - , subq_9.metric_time__extract_quarter - , subq_9.metric_time__extract_month - , subq_9.metric_time__extract_day - , subq_9.metric_time__extract_dow - , subq_9.metric_time__extract_doy - , subq_9.user - , subq_9.session_id - , subq_9.buy__user - , subq_9.buy__session_id - , subq_9.buys - , subq_9.buyers + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.metric_time__day + , subq_10.metric_time__week + , subq_10.metric_time__month + , subq_10.metric_time__quarter + , subq_10.metric_time__year + , subq_10.metric_time__extract_year + , subq_10.metric_time__extract_quarter + , subq_10.metric_time__extract_month + , subq_10.metric_time__extract_day + , subq_10.metric_time__extract_dow + , subq_10.metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_8.ds__day - , subq_8.ds__week - , subq_8.ds__month - , subq_8.ds__quarter - , subq_8.ds__year - , subq_8.ds__extract_year - , subq_8.ds__extract_quarter - , subq_8.ds__extract_month - , subq_8.ds__extract_day - , subq_8.ds__extract_dow - , subq_8.ds__extract_doy - , subq_8.buy__ds__day - , subq_8.buy__ds__week - , subq_8.buy__ds__month - , subq_8.buy__ds__quarter - , subq_8.buy__ds__year - , subq_8.buy__ds__extract_year - , subq_8.buy__ds__extract_quarter - , subq_8.buy__ds__extract_month - , subq_8.buy__ds__extract_day - , subq_8.buy__ds__extract_dow - , subq_8.buy__ds__extract_doy - , subq_8.ds__day AS metric_time__day - , subq_8.ds__week AS metric_time__week - , subq_8.ds__month AS metric_time__month - , subq_8.ds__quarter AS metric_time__quarter - , subq_8.ds__year AS metric_time__year - , subq_8.ds__extract_year AS metric_time__extract_year - , subq_8.ds__extract_quarter AS metric_time__extract_quarter - , subq_8.ds__extract_month AS metric_time__extract_month - , subq_8.ds__extract_day AS metric_time__extract_day - , subq_8.ds__extract_dow AS metric_time__extract_dow - , subq_8.ds__extract_doy AS metric_time__extract_doy - , subq_8.user - , subq_8.session_id - , subq_8.buy__user - , subq_8.buy__session_id - , subq_8.buys - , subq_8.buyers + subq_9.ds__day + , subq_9.ds__week + , subq_9.ds__month + , subq_9.ds__quarter + , subq_9.ds__year + , subq_9.ds__extract_year + , subq_9.ds__extract_quarter + , subq_9.ds__extract_month + , subq_9.ds__extract_day + , subq_9.ds__extract_dow + , subq_9.ds__extract_doy + , subq_9.buy__ds__day + , subq_9.buy__ds__week + , subq_9.buy__ds__month + , subq_9.buy__ds__quarter + , subq_9.buy__ds__year + , subq_9.buy__ds__extract_year + , subq_9.buy__ds__extract_quarter + , subq_9.buy__ds__extract_month + , subq_9.buy__ds__extract_day + , subq_9.buy__ds__extract_dow + , subq_9.buy__ds__extract_doy + , subq_9.ds__day AS metric_time__day + , subq_9.ds__week AS metric_time__week + , subq_9.ds__month AS metric_time__month + , subq_9.ds__quarter AS metric_time__quarter + , subq_9.ds__year AS metric_time__year + , subq_9.ds__extract_year AS metric_time__extract_year + , subq_9.ds__extract_quarter AS metric_time__extract_quarter + , subq_9.ds__extract_month AS metric_time__extract_month + , subq_9.ds__extract_day AS metric_time__extract_day + , subq_9.ds__extract_dow AS metric_time__extract_dow + , subq_9.ds__extract_doy AS metric_time__extract_doy + , subq_9.user + , subq_9.session_id + , subq_9.buy__user + , subq_9.buy__session_id + , subq_9.buys + , subq_9.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -387,29 +433,29 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_8 - ) subq_9 - ) subq_10 + ) subq_9 + ) subq_10 + ) subq_11 ON ( - subq_7.user = subq_10.user + subq_8.user = subq_11.user ) AND ( - (subq_7.ds__day <= subq_10.ds__day) + (subq_8.ds__day <= subq_11.ds__day) ) - ) subq_11 - ) subq_12 - ) subq_13 + ) subq_12 + ) subq_13 + ) subq_14 GROUP BY - subq_13.metric_time__day - , subq_13.visit__referrer_id - ) subq_14 + subq_14.metric_time__day + , subq_14.visit__referrer_id + ) subq_15 ON ( - subq_4.visit__referrer_id = subq_14.visit__referrer_id + subq_4.visit__referrer_id = subq_15.visit__referrer_id ) AND ( - subq_4.metric_time__day = subq_14.metric_time__day + subq_4.metric_time__day = subq_15.metric_time__day ) GROUP BY - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) - , COALESCE(subq_4.visit__referrer_id, subq_14.visit__referrer_id) -) subq_15 + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) + , COALESCE(subq_4.visit__referrer_id, subq_15.visit__referrer_id) +) subq_16 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_categorical_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_categorical_filter__plan0_optimized.sql index b06e52d6cf..492cbfbe44 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_categorical_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_categorical_filter__plan0_optimized.sql @@ -6,10 +6,10 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , COALESCE(subq_20.visit__referrer_id, subq_30.visit__referrer_id) AS visit__referrer_id - , MAX(subq_20.visits) AS visits - , MAX(subq_30.buys) AS buys + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , COALESCE(subq_21.visit__referrer_id, subq_32.visit__referrer_id) AS visit__referrer_id + , MAX(subq_21.visits) AS visits + , MAX(subq_32.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -26,12 +26,12 @@ FROM ( , referrer_id AS visit__referrer_id , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_18 + ) subq_19 WHERE visit__referrer_id = 'ref_id_01' GROUP BY metric_time__day , visit__referrer_id - ) subq_20 + ) subq_21 FULL OUTER JOIN ( -- Find conversions for user within the range of INF -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] @@ -43,60 +43,70 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_23.visits) OVER ( + FIRST_VALUE(subq_25.visits) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_23.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_25.visit__referrer_id) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_23.ds__day) OVER ( + , FIRST_VALUE(subq_25.ds__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_23.metric_time__day) OVER ( + , FIRST_VALUE(subq_25.metric_time__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_23.user) OVER ( + , FIRST_VALUE(subq_25.user) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_26.mf_internal_uuid AS mf_internal_uuid - , subq_26.buys AS buys + , subq_28.mf_internal_uuid AS mf_internal_uuid + , subq_28.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + ds__day + , metric_time__day + , subq_23.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_23 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_25 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -107,25 +117,25 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_26 + ) subq_28 ON ( - subq_23.user = subq_26.user + subq_25.user = subq_28.user ) AND ( - (subq_23.ds__day <= subq_26.ds__day) + (subq_25.ds__day <= subq_28.ds__day) ) - ) subq_27 + ) subq_29 GROUP BY metric_time__day , visit__referrer_id - ) subq_30 + ) subq_32 ON ( - subq_20.visit__referrer_id = subq_30.visit__referrer_id + subq_21.visit__referrer_id = subq_32.visit__referrer_id ) AND ( - subq_20.metric_time__day = subq_30.metric_time__day + subq_21.metric_time__day = subq_32.metric_time__day ) GROUP BY - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) - , COALESCE(subq_20.visit__referrer_id, subq_30.visit__referrer_id) -) subq_31 + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) + , COALESCE(subq_21.visit__referrer_id, subq_32.visit__referrer_id) +) subq_33 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_filter__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_filter__plan0.sql new file mode 100644 index 0000000000..624848d4f7 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_filter__plan0.sql @@ -0,0 +1,429 @@ +-- Compute Metrics via Expressions +SELECT + CAST(subq_17.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_17.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_5.visits) AS visits + , MAX(subq_16.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_4.visits) AS visits + FROM ( + -- Pass Only Elements: ['visits',] + SELECT + subq_3.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_2.metric_time__day + , subq_2.visits + FROM ( + -- Pass Only Elements: ['visits', 'metric_time__day'] + SELECT + subq_1.metric_time__day + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_0 + ) subq_1 + ) subq_2 + WHERE metric_time__day = '2020-01-01' + ) subq_3 + ) subq_4 + ) subq_5 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_15.buys) AS buys + FROM ( + -- Pass Only Elements: ['buys',] + SELECT + subq_14.buys + FROM ( + -- Find conversions for user within the range of INF + SELECT + subq_13.ds__day + , subq_13.metric_time__day + , subq_13.user + , subq_13.buys + , subq_13.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_9.visits) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_9.ds__day) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_9.metric_time__day) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS metric_time__day + , FIRST_VALUE(subq_9.user) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_12.mf_internal_uuid AS mf_internal_uuid + , subq_12.buys AS buys + FROM ( + -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] + SELECT + subq_8.ds__day + , subq_8.metric_time__day + , subq_8.user + , subq_8.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE metric_time__day = '2020-01-01' + ) subq_8 + ) subq_9 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.metric_time__day + , subq_11.metric_time__week + , subq_11.metric_time__month + , subq_11.metric_time__quarter + , subq_11.metric_time__year + , subq_11.metric_time__extract_year + , subq_11.metric_time__extract_quarter + , subq_11.metric_time__extract_month + , subq_11.metric_time__extract_day + , subq_11.metric_time__extract_dow + , subq_11.metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.ds__day AS metric_time__day + , subq_10.ds__week AS metric_time__week + , subq_10.ds__month AS metric_time__month + , subq_10.ds__quarter AS metric_time__quarter + , subq_10.ds__year AS metric_time__year + , subq_10.ds__extract_year AS metric_time__extract_year + , subq_10.ds__extract_quarter AS metric_time__extract_quarter + , subq_10.ds__extract_month AS metric_time__extract_month + , subq_10.ds__extract_day AS metric_time__extract_day + , subq_10.ds__extract_dow AS metric_time__extract_dow + , subq_10.ds__extract_doy AS metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_28000.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM buys_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_28000.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day + , EXTRACT(isodow FROM buys_source_src_28000.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS buy__ds__extract_doy + , buys_source_src_28000.user_id AS user + , buys_source_src_28000.session_id + , buys_source_src_28000.user_id AS buy__user + , buys_source_src_28000.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_10 + ) subq_11 + ) subq_12 + ON + ( + subq_9.user = subq_12.user + ) AND ( + (subq_9.ds__day <= subq_12.ds__day) + ) + ) subq_13 + ) subq_14 + ) subq_15 + ) subq_16 +) subq_17 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_filter__plan0_optimized.sql new file mode 100644 index 0000000000..8d82b11364 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_filter__plan0_optimized.sql @@ -0,0 +1,103 @@ +-- Combine Aggregated Outputs +-- Compute Metrics via Expressions +SELECT + CAST(MAX(subq_34.buys) AS DOUBLE PRECISION) / CAST(NULLIF(MAX(subq_23.visits), 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate +FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits',] + -- Aggregate Measures + SELECT + 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_20 + WHERE metric_time__day = '2020-01-01' +) subq_23 +CROSS JOIN ( + -- Find conversions for user within the range of INF + -- Pass Only Elements: ['buys',] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_27.visits) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_27.ds__day) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_27.metric_time__day) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS metric_time__day + , FIRST_VALUE(subq_27.user) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_30.mf_internal_uuid AS mf_internal_uuid + , subq_30.buys AS buys + FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] + SELECT + ds__day + , metric_time__day + , subq_25.user + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_25 + WHERE metric_time__day = '2020-01-01' + ) subq_27 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_30 + ON + ( + subq_27.user = subq_30.user + ) AND ( + (subq_27.ds__day <= subq_30.ds__day) + ) + ) subq_31 +) subq_34 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_filter_not_in_group_by__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_filter_not_in_group_by__plan0.sql new file mode 100644 index 0000000000..fa78370575 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_filter_not_in_group_by__plan0.sql @@ -0,0 +1,433 @@ +-- Compute Metrics via Expressions +SELECT + subq_17.buys AS visit_buy_conversions +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_5.visits) AS visits + , COALESCE(MAX(subq_16.buys), 0) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_4.visits) AS visits + FROM ( + -- Pass Only Elements: ['visits',] + SELECT + subq_3.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_2.visit__referrer_id + , subq_2.visits + FROM ( + -- Pass Only Elements: ['visits', 'visit__referrer_id'] + SELECT + subq_1.visit__referrer_id + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_0 + ) subq_1 + ) subq_2 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_3 + ) subq_4 + ) subq_5 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_15.buys) AS buys + FROM ( + -- Pass Only Elements: ['buys',] + SELECT + subq_14.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_13.ds__day + , subq_13.user + , subq_13.visit__referrer_id + , subq_13.buys + , subq_13.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_9.visits) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_9.visit__referrer_id) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visit__referrer_id + , FIRST_VALUE(subq_9.ds__day) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_9.user) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_12.mf_internal_uuid AS mf_internal_uuid + , subq_12.buys AS buys + FROM ( + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] + SELECT + subq_8.ds__day + , subq_8.user + , subq_8.visit__referrer_id + , subq_8.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_8 + ) subq_9 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.metric_time__day + , subq_11.metric_time__week + , subq_11.metric_time__month + , subq_11.metric_time__quarter + , subq_11.metric_time__year + , subq_11.metric_time__extract_year + , subq_11.metric_time__extract_quarter + , subq_11.metric_time__extract_month + , subq_11.metric_time__extract_day + , subq_11.metric_time__extract_dow + , subq_11.metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.ds__day AS metric_time__day + , subq_10.ds__week AS metric_time__week + , subq_10.ds__month AS metric_time__month + , subq_10.ds__quarter AS metric_time__quarter + , subq_10.ds__year AS metric_time__year + , subq_10.ds__extract_year AS metric_time__extract_year + , subq_10.ds__extract_quarter AS metric_time__extract_quarter + , subq_10.ds__extract_month AS metric_time__extract_month + , subq_10.ds__extract_day AS metric_time__extract_day + , subq_10.ds__extract_dow AS metric_time__extract_dow + , subq_10.ds__extract_doy AS metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_28000.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM buys_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_28000.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day + , EXTRACT(isodow FROM buys_source_src_28000.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS buy__ds__extract_doy + , buys_source_src_28000.user_id AS user + , buys_source_src_28000.session_id + , buys_source_src_28000.user_id AS buy__user + , buys_source_src_28000.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_10 + ) subq_11 + ) subq_12 + ON + ( + subq_9.user = subq_12.user + ) AND ( + ( + subq_9.ds__day <= subq_12.ds__day + ) AND ( + subq_9.ds__day > subq_12.ds__day - MAKE_INTERVAL(days => 7) + ) + ) + ) subq_13 + ) subq_14 + ) subq_15 + ) subq_16 +) subq_17 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_filter_not_in_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_filter_not_in_group_by__plan0_optimized.sql new file mode 100644 index 0000000000..0c6201d472 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_filter_not_in_group_by__plan0_optimized.sql @@ -0,0 +1,107 @@ +-- Combine Aggregated Outputs +-- Compute Metrics via Expressions +SELECT + COALESCE(MAX(subq_34.buys), 0) AS visit_buy_conversions +FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits',] + -- Aggregate Measures + SELECT + 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_20 + WHERE visit__referrer_id = 'ref_id_01' +) subq_23 +CROSS JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: ['buys',] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_27.visits) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_27.visit__referrer_id) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visit__referrer_id + , FIRST_VALUE(subq_27.ds__day) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_27.user) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_30.mf_internal_uuid AS mf_internal_uuid + , subq_30.buys AS buys + FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] + SELECT + ds__day + , subq_25.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_25 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_27 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_30 + ON + ( + subq_27.user = subq_30.user + ) AND ( + ( + subq_27.ds__day <= subq_30.ds__day + ) AND ( + subq_27.ds__day > subq_30.ds__day - MAKE_INTERVAL(days => 7) + ) + ) + ) subq_31 +) subq_34 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_time_constraint__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_time_constraint__plan0.sql index 089a2af327..2962a64f63 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_time_constraint__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_time_constraint__plan0.sql @@ -1,13 +1,13 @@ -- Compute Metrics via Expressions SELECT - subq_17.visit__referrer_id - , CAST(subq_17.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_17.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate + subq_18.visit__referrer_id + , CAST(subq_18.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_18.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) AS visit__referrer_id , MAX(subq_5.visits) AS visits - , MAX(subq_16.buys) AS buys + , MAX(subq_17.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -158,277 +158,323 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_15.visit__referrer_id - , SUM(subq_15.buys) AS buys + subq_16.visit__referrer_id + , SUM(subq_16.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'visit__referrer_id'] SELECT - subq_14.visit__referrer_id - , subq_14.buys + subq_15.visit__referrer_id + , subq_15.buys FROM ( -- Find conversions for user within the range of INF SELECT - subq_13.ds__day - , subq_13.user - , subq_13.visit__referrer_id - , subq_13.buys - , subq_13.visits + subq_14.ds__day + , subq_14.user + , subq_14.visit__referrer_id + , subq_14.buys + , subq_14.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_9.visits) OVER ( + FIRST_VALUE(subq_10.visits) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_9.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_10.visit__referrer_id) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_9.ds__day) OVER ( + , FIRST_VALUE(subq_10.ds__day) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_9.user) OVER ( + , FIRST_VALUE(subq_10.user) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_12.mf_internal_uuid AS mf_internal_uuid - , subq_12.buys AS buys + , subq_13.mf_internal_uuid AS mf_internal_uuid + , subq_13.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] SELECT - subq_8.ds__day - , subq_8.user - , subq_8.visit__referrer_id - , subq_8.visits + subq_9.ds__day + , subq_9.user + , subq_9.visit__referrer_id + , subq_9.visits FROM ( - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE SELECT - subq_7.ds__day - , subq_7.ds__week - , subq_7.ds__month - , subq_7.ds__quarter - , subq_7.ds__year - , subq_7.ds__extract_year - , subq_7.ds__extract_quarter - , subq_7.ds__extract_month - , subq_7.ds__extract_day - , subq_7.ds__extract_dow - , subq_7.ds__extract_doy - , subq_7.visit__ds__day - , subq_7.visit__ds__week - , subq_7.visit__ds__month - , subq_7.visit__ds__quarter - , subq_7.visit__ds__year - , subq_7.visit__ds__extract_year - , subq_7.visit__ds__extract_quarter - , subq_7.visit__ds__extract_month - , subq_7.visit__ds__extract_day - , subq_7.visit__ds__extract_dow - , subq_7.visit__ds__extract_doy - , subq_7.metric_time__day - , subq_7.metric_time__week - , subq_7.metric_time__month - , subq_7.metric_time__quarter - , subq_7.metric_time__year - , subq_7.metric_time__extract_year - , subq_7.metric_time__extract_quarter - , subq_7.metric_time__extract_month - , subq_7.metric_time__extract_day - , subq_7.metric_time__extract_dow - , subq_7.metric_time__extract_doy - , subq_7.user - , subq_7.session - , subq_7.visit__user - , subq_7.visit__session - , subq_7.referrer_id - , subq_7.visit__referrer_id - , subq_7.visits - , subq_7.visitors + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.visit__ds__day + , subq_8.visit__ds__week + , subq_8.visit__ds__month + , subq_8.visit__ds__quarter + , subq_8.visit__ds__year + , subq_8.visit__ds__extract_year + , subq_8.visit__ds__extract_quarter + , subq_8.visit__ds__extract_month + , subq_8.visit__ds__extract_day + , subq_8.visit__ds__extract_dow + , subq_8.visit__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session + , subq_8.visit__user + , subq_8.visit__session + , subq_8.referrer_id + , subq_8.visit__referrer_id + , subq_8.visits + , subq_8.visitors FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] SELECT - subq_6.ds__day - , subq_6.ds__week - , subq_6.ds__month - , subq_6.ds__quarter - , subq_6.ds__year - , subq_6.ds__extract_year - , subq_6.ds__extract_quarter - , subq_6.ds__extract_month - , subq_6.ds__extract_day - , subq_6.ds__extract_dow - , subq_6.ds__extract_doy - , subq_6.visit__ds__day - , subq_6.visit__ds__week - , subq_6.visit__ds__month - , subq_6.visit__ds__quarter - , subq_6.visit__ds__year - , subq_6.visit__ds__extract_year - , subq_6.visit__ds__extract_quarter - , subq_6.visit__ds__extract_month - , subq_6.visit__ds__extract_day - , subq_6.visit__ds__extract_dow - , subq_6.visit__ds__extract_doy - , subq_6.ds__day AS metric_time__day - , subq_6.ds__week AS metric_time__week - , subq_6.ds__month AS metric_time__month - , subq_6.ds__quarter AS metric_time__quarter - , subq_6.ds__year AS metric_time__year - , subq_6.ds__extract_year AS metric_time__extract_year - , subq_6.ds__extract_quarter AS metric_time__extract_quarter - , subq_6.ds__extract_month AS metric_time__extract_month - , subq_6.ds__extract_day AS metric_time__extract_day - , subq_6.ds__extract_dow AS metric_time__extract_dow - , subq_6.ds__extract_doy AS metric_time__extract_doy - , subq_6.user - , subq_6.session - , subq_6.visit__user - , subq_6.visit__session - , subq_6.referrer_id - , subq_6.visit__referrer_id - , subq_6.visits - , subq_6.visitors + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_6 - ) subq_7 - WHERE subq_7.metric_time__day BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_8 - ) subq_9 + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE subq_7.metric_time__day BETWEEN '2020-01-01' AND '2020-01-02' + ) subq_8 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_9 + ) subq_10 INNER JOIN ( -- Add column with generated UUID SELECT - subq_11.ds__day - , subq_11.ds__week - , subq_11.ds__month - , subq_11.ds__quarter - , subq_11.ds__year - , subq_11.ds__extract_year - , subq_11.ds__extract_quarter - , subq_11.ds__extract_month - , subq_11.ds__extract_day - , subq_11.ds__extract_dow - , subq_11.ds__extract_doy - , subq_11.buy__ds__day - , subq_11.buy__ds__week - , subq_11.buy__ds__month - , subq_11.buy__ds__quarter - , subq_11.buy__ds__year - , subq_11.buy__ds__extract_year - , subq_11.buy__ds__extract_quarter - , subq_11.buy__ds__extract_month - , subq_11.buy__ds__extract_day - , subq_11.buy__ds__extract_dow - , subq_11.buy__ds__extract_doy - , subq_11.metric_time__day - , subq_11.metric_time__week - , subq_11.metric_time__month - , subq_11.metric_time__quarter - , subq_11.metric_time__year - , subq_11.metric_time__extract_year - , subq_11.metric_time__extract_quarter - , subq_11.metric_time__extract_month - , subq_11.metric_time__extract_day - , subq_11.metric_time__extract_dow - , subq_11.metric_time__extract_doy - , subq_11.user - , subq_11.session_id - , subq_11.buy__user - , subq_11.buy__session_id - , subq_11.buys - , subq_11.buyers + subq_12.ds__day + , subq_12.ds__week + , subq_12.ds__month + , subq_12.ds__quarter + , subq_12.ds__year + , subq_12.ds__extract_year + , subq_12.ds__extract_quarter + , subq_12.ds__extract_month + , subq_12.ds__extract_day + , subq_12.ds__extract_dow + , subq_12.ds__extract_doy + , subq_12.buy__ds__day + , subq_12.buy__ds__week + , subq_12.buy__ds__month + , subq_12.buy__ds__quarter + , subq_12.buy__ds__year + , subq_12.buy__ds__extract_year + , subq_12.buy__ds__extract_quarter + , subq_12.buy__ds__extract_month + , subq_12.buy__ds__extract_day + , subq_12.buy__ds__extract_dow + , subq_12.buy__ds__extract_doy + , subq_12.metric_time__day + , subq_12.metric_time__week + , subq_12.metric_time__month + , subq_12.metric_time__quarter + , subq_12.metric_time__year + , subq_12.metric_time__extract_year + , subq_12.metric_time__extract_quarter + , subq_12.metric_time__extract_month + , subq_12.metric_time__extract_day + , subq_12.metric_time__extract_dow + , subq_12.metric_time__extract_doy + , subq_12.user + , subq_12.session_id + , subq_12.buy__user + , subq_12.buy__session_id + , subq_12.buys + , subq_12.buyers , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_10.ds__day - , subq_10.ds__week - , subq_10.ds__month - , subq_10.ds__quarter - , subq_10.ds__year - , subq_10.ds__extract_year - , subq_10.ds__extract_quarter - , subq_10.ds__extract_month - , subq_10.ds__extract_day - , subq_10.ds__extract_dow - , subq_10.ds__extract_doy - , subq_10.buy__ds__day - , subq_10.buy__ds__week - , subq_10.buy__ds__month - , subq_10.buy__ds__quarter - , subq_10.buy__ds__year - , subq_10.buy__ds__extract_year - , subq_10.buy__ds__extract_quarter - , subq_10.buy__ds__extract_month - , subq_10.buy__ds__extract_day - , subq_10.buy__ds__extract_dow - , subq_10.buy__ds__extract_doy - , subq_10.ds__day AS metric_time__day - , subq_10.ds__week AS metric_time__week - , subq_10.ds__month AS metric_time__month - , subq_10.ds__quarter AS metric_time__quarter - , subq_10.ds__year AS metric_time__year - , subq_10.ds__extract_year AS metric_time__extract_year - , subq_10.ds__extract_quarter AS metric_time__extract_quarter - , subq_10.ds__extract_month AS metric_time__extract_month - , subq_10.ds__extract_day AS metric_time__extract_day - , subq_10.ds__extract_dow AS metric_time__extract_dow - , subq_10.ds__extract_doy AS metric_time__extract_doy - , subq_10.user - , subq_10.session_id - , subq_10.buy__user - , subq_10.buy__session_id - , subq_10.buys - , subq_10.buyers + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.ds__day AS metric_time__day + , subq_11.ds__week AS metric_time__week + , subq_11.ds__month AS metric_time__month + , subq_11.ds__quarter AS metric_time__quarter + , subq_11.ds__year AS metric_time__year + , subq_11.ds__extract_year AS metric_time__extract_year + , subq_11.ds__extract_quarter AS metric_time__extract_quarter + , subq_11.ds__extract_month AS metric_time__extract_month + , subq_11.ds__extract_day AS metric_time__extract_day + , subq_11.ds__extract_dow AS metric_time__extract_dow + , subq_11.ds__extract_doy AS metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -461,23 +507,23 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_10 - ) subq_11 - ) subq_12 + ) subq_11 + ) subq_12 + ) subq_13 ON ( - subq_9.user = subq_12.user + subq_10.user = subq_13.user ) AND ( - (subq_9.ds__day <= subq_12.ds__day) + (subq_10.ds__day <= subq_13.ds__day) ) - ) subq_13 - ) subq_14 - ) subq_15 + ) subq_14 + ) subq_15 + ) subq_16 GROUP BY - subq_15.visit__referrer_id - ) subq_16 + subq_16.visit__referrer_id + ) subq_17 ON - subq_5.visit__referrer_id = subq_16.visit__referrer_id + subq_5.visit__referrer_id = subq_17.visit__referrer_id GROUP BY - COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) -) subq_17 + COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) +) subq_18 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_time_constraint__plan0_optimized.sql index 9a873c7c98..203faafa7a 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_time_constraint__plan0_optimized.sql @@ -5,9 +5,9 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) AS visit__referrer_id - , MAX(subq_23.visits) AS visits - , MAX(subq_34.buys) AS buys + COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) AS visit__referrer_id + , MAX(subq_24.visits) AS visits + , MAX(subq_36.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -24,11 +24,11 @@ FROM ( , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_21 + ) subq_22 WHERE visit__referrer_id = 'ref_id_01' GROUP BY visit__referrer_id - ) subq_23 + ) subq_24 FULL OUTER JOIN ( -- Find conversions for user within the range of INF -- Pass Only Elements: ['buys', 'visit__referrer_id'] @@ -39,53 +39,62 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_27.visits) OVER ( + FIRST_VALUE(subq_29.visits) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_27.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_29.visit__referrer_id) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_27.ds__day) OVER ( + , FIRST_VALUE(subq_29.ds__day) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_27.user) OVER ( + , FIRST_VALUE(subq_29.user) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_30.mf_internal_uuid AS mf_internal_uuid - , subq_30.buys AS buys + , subq_32.mf_internal_uuid AS mf_internal_uuid + , subq_32.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_27 + ds__day + , subq_27.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' + ) subq_27 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_29 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -96,19 +105,19 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_30 + ) subq_32 ON ( - subq_27.user = subq_30.user + subq_29.user = subq_32.user ) AND ( - (subq_27.ds__day <= subq_30.ds__day) + (subq_29.ds__day <= subq_32.ds__day) ) - ) subq_31 + ) subq_33 GROUP BY visit__referrer_id - ) subq_34 + ) subq_36 ON - subq_23.visit__referrer_id = subq_34.visit__referrer_id + subq_24.visit__referrer_id = subq_36.visit__referrer_id GROUP BY - COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) -) subq_35 + COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) +) subq_37 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_window__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_window__plan0.sql index f88c4a8812..a02d0ebc27 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_window__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_window__plan0.sql @@ -1,13 +1,13 @@ -- Compute Metrics via Expressions SELECT - subq_15.metric_time__day - , CAST(subq_15.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_15.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_7days + subq_16.metric_time__day + , CAST(subq_16.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_16.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) AS metric_time__day + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) AS metric_time__day , MAX(subq_4.visits) AS visits - , MAX(subq_14.buys) AS buys + , MAX(subq_15.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -112,231 +112,277 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_13.metric_time__day - , SUM(subq_13.buys) AS buys + subq_14.metric_time__day + , SUM(subq_14.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'metric_time__day'] SELECT - subq_12.metric_time__day - , subq_12.buys + subq_13.metric_time__day + , subq_13.buys FROM ( -- Find conversions for user within the range of 7 day SELECT - subq_11.ds__day - , subq_11.metric_time__day - , subq_11.user - , subq_11.buys - , subq_11.visits + subq_12.ds__day + , subq_12.metric_time__day + , subq_12.user + , subq_12.buys + , subq_12.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_7.visits) OVER ( + FIRST_VALUE(subq_8.visits) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_7.ds__day) OVER ( + , FIRST_VALUE(subq_8.ds__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_7.metric_time__day) OVER ( + , FIRST_VALUE(subq_8.metric_time__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_7.user) OVER ( + , FIRST_VALUE(subq_8.user) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_10.mf_internal_uuid AS mf_internal_uuid - , subq_10.buys AS buys + , subq_11.mf_internal_uuid AS mf_internal_uuid + , subq_11.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_6.ds__day - , subq_6.metric_time__day - , subq_6.user - , subq_6.visits + subq_7.ds__day + , subq_7.metric_time__day + , subq_7.user + , subq_7.visits FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE SELECT - subq_5.ds__day - , subq_5.ds__week - , subq_5.ds__month - , subq_5.ds__quarter - , subq_5.ds__year - , subq_5.ds__extract_year - , subq_5.ds__extract_quarter - , subq_5.ds__extract_month - , subq_5.ds__extract_day - , subq_5.ds__extract_dow - , subq_5.ds__extract_doy - , subq_5.visit__ds__day - , subq_5.visit__ds__week - , subq_5.visit__ds__month - , subq_5.visit__ds__quarter - , subq_5.visit__ds__year - , subq_5.visit__ds__extract_year - , subq_5.visit__ds__extract_quarter - , subq_5.visit__ds__extract_month - , subq_5.visit__ds__extract_day - , subq_5.visit__ds__extract_dow - , subq_5.visit__ds__extract_doy - , subq_5.ds__day AS metric_time__day - , subq_5.ds__week AS metric_time__week - , subq_5.ds__month AS metric_time__month - , subq_5.ds__quarter AS metric_time__quarter - , subq_5.ds__year AS metric_time__year - , subq_5.ds__extract_year AS metric_time__extract_year - , subq_5.ds__extract_quarter AS metric_time__extract_quarter - , subq_5.ds__extract_month AS metric_time__extract_month - , subq_5.ds__extract_day AS metric_time__extract_day - , subq_5.ds__extract_dow AS metric_time__extract_dow - , subq_5.ds__extract_doy AS metric_time__extract_doy - , subq_5.user - , subq_5.session - , subq_5.visit__user - , subq_5.visit__session - , subq_5.referrer_id - , subq_5.visit__referrer_id - , subq_5.visits - , subq_5.visitors + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.metric_time__day + , subq_6.metric_time__week + , subq_6.metric_time__month + , subq_6.metric_time__quarter + , subq_6.metric_time__year + , subq_6.metric_time__extract_year + , subq_6.metric_time__extract_quarter + , subq_6.metric_time__extract_month + , subq_6.metric_time__extract_day + , subq_6.metric_time__extract_dow + , subq_6.metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_5 - ) subq_6 - ) subq_7 + subq_5.ds__day + , subq_5.ds__week + , subq_5.ds__month + , subq_5.ds__quarter + , subq_5.ds__year + , subq_5.ds__extract_year + , subq_5.ds__extract_quarter + , subq_5.ds__extract_month + , subq_5.ds__extract_day + , subq_5.ds__extract_dow + , subq_5.ds__extract_doy + , subq_5.visit__ds__day + , subq_5.visit__ds__week + , subq_5.visit__ds__month + , subq_5.visit__ds__quarter + , subq_5.visit__ds__year + , subq_5.visit__ds__extract_year + , subq_5.visit__ds__extract_quarter + , subq_5.visit__ds__extract_month + , subq_5.visit__ds__extract_day + , subq_5.visit__ds__extract_dow + , subq_5.visit__ds__extract_doy + , subq_5.ds__day AS metric_time__day + , subq_5.ds__week AS metric_time__week + , subq_5.ds__month AS metric_time__month + , subq_5.ds__quarter AS metric_time__quarter + , subq_5.ds__year AS metric_time__year + , subq_5.ds__extract_year AS metric_time__extract_year + , subq_5.ds__extract_quarter AS metric_time__extract_quarter + , subq_5.ds__extract_month AS metric_time__extract_month + , subq_5.ds__extract_day AS metric_time__extract_day + , subq_5.ds__extract_dow AS metric_time__extract_dow + , subq_5.ds__extract_doy AS metric_time__extract_doy + , subq_5.user + , subq_5.session + , subq_5.visit__user + , subq_5.visit__session + , subq_5.referrer_id + , subq_5.visit__referrer_id + , subq_5.visits + , subq_5.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_5 + ) subq_6 + WHERE metric_time__day = '2020-01-01' + ) subq_7 + ) subq_8 INNER JOIN ( -- Add column with generated UUID SELECT - subq_9.ds__day - , subq_9.ds__week - , subq_9.ds__month - , subq_9.ds__quarter - , subq_9.ds__year - , subq_9.ds__extract_year - , subq_9.ds__extract_quarter - , subq_9.ds__extract_month - , subq_9.ds__extract_day - , subq_9.ds__extract_dow - , subq_9.ds__extract_doy - , subq_9.buy__ds__day - , subq_9.buy__ds__week - , subq_9.buy__ds__month - , subq_9.buy__ds__quarter - , subq_9.buy__ds__year - , subq_9.buy__ds__extract_year - , subq_9.buy__ds__extract_quarter - , subq_9.buy__ds__extract_month - , subq_9.buy__ds__extract_day - , subq_9.buy__ds__extract_dow - , subq_9.buy__ds__extract_doy - , subq_9.metric_time__day - , subq_9.metric_time__week - , subq_9.metric_time__month - , subq_9.metric_time__quarter - , subq_9.metric_time__year - , subq_9.metric_time__extract_year - , subq_9.metric_time__extract_quarter - , subq_9.metric_time__extract_month - , subq_9.metric_time__extract_day - , subq_9.metric_time__extract_dow - , subq_9.metric_time__extract_doy - , subq_9.user - , subq_9.session_id - , subq_9.buy__user - , subq_9.buy__session_id - , subq_9.buys - , subq_9.buyers + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.metric_time__day + , subq_10.metric_time__week + , subq_10.metric_time__month + , subq_10.metric_time__quarter + , subq_10.metric_time__year + , subq_10.metric_time__extract_year + , subq_10.metric_time__extract_quarter + , subq_10.metric_time__extract_month + , subq_10.metric_time__extract_day + , subq_10.metric_time__extract_dow + , subq_10.metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_8.ds__day - , subq_8.ds__week - , subq_8.ds__month - , subq_8.ds__quarter - , subq_8.ds__year - , subq_8.ds__extract_year - , subq_8.ds__extract_quarter - , subq_8.ds__extract_month - , subq_8.ds__extract_day - , subq_8.ds__extract_dow - , subq_8.ds__extract_doy - , subq_8.buy__ds__day - , subq_8.buy__ds__week - , subq_8.buy__ds__month - , subq_8.buy__ds__quarter - , subq_8.buy__ds__year - , subq_8.buy__ds__extract_year - , subq_8.buy__ds__extract_quarter - , subq_8.buy__ds__extract_month - , subq_8.buy__ds__extract_day - , subq_8.buy__ds__extract_dow - , subq_8.buy__ds__extract_doy - , subq_8.ds__day AS metric_time__day - , subq_8.ds__week AS metric_time__week - , subq_8.ds__month AS metric_time__month - , subq_8.ds__quarter AS metric_time__quarter - , subq_8.ds__year AS metric_time__year - , subq_8.ds__extract_year AS metric_time__extract_year - , subq_8.ds__extract_quarter AS metric_time__extract_quarter - , subq_8.ds__extract_month AS metric_time__extract_month - , subq_8.ds__extract_day AS metric_time__extract_day - , subq_8.ds__extract_dow AS metric_time__extract_dow - , subq_8.ds__extract_doy AS metric_time__extract_doy - , subq_8.user - , subq_8.session_id - , subq_8.buy__user - , subq_8.buy__session_id - , subq_8.buys - , subq_8.buyers + subq_9.ds__day + , subq_9.ds__week + , subq_9.ds__month + , subq_9.ds__quarter + , subq_9.ds__year + , subq_9.ds__extract_year + , subq_9.ds__extract_quarter + , subq_9.ds__extract_month + , subq_9.ds__extract_day + , subq_9.ds__extract_dow + , subq_9.ds__extract_doy + , subq_9.buy__ds__day + , subq_9.buy__ds__week + , subq_9.buy__ds__month + , subq_9.buy__ds__quarter + , subq_9.buy__ds__year + , subq_9.buy__ds__extract_year + , subq_9.buy__ds__extract_quarter + , subq_9.buy__ds__extract_month + , subq_9.buy__ds__extract_day + , subq_9.buy__ds__extract_dow + , subq_9.buy__ds__extract_doy + , subq_9.ds__day AS metric_time__day + , subq_9.ds__week AS metric_time__week + , subq_9.ds__month AS metric_time__month + , subq_9.ds__quarter AS metric_time__quarter + , subq_9.ds__year AS metric_time__year + , subq_9.ds__extract_year AS metric_time__extract_year + , subq_9.ds__extract_quarter AS metric_time__extract_quarter + , subq_9.ds__extract_month AS metric_time__extract_month + , subq_9.ds__extract_day AS metric_time__extract_day + , subq_9.ds__extract_dow AS metric_time__extract_dow + , subq_9.ds__extract_doy AS metric_time__extract_doy + , subq_9.user + , subq_9.session_id + , subq_9.buy__user + , subq_9.buy__session_id + , subq_9.buys + , subq_9.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -369,27 +415,27 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_8 - ) subq_9 - ) subq_10 + ) subq_9 + ) subq_10 + ) subq_11 ON ( - subq_7.user = subq_10.user + subq_8.user = subq_11.user ) AND ( ( - subq_7.ds__day <= subq_10.ds__day + subq_8.ds__day <= subq_11.ds__day ) AND ( - subq_7.ds__day > subq_10.ds__day - MAKE_INTERVAL(days => 7) + subq_8.ds__day > subq_11.ds__day - MAKE_INTERVAL(days => 7) ) ) - ) subq_11 - ) subq_12 - ) subq_13 + ) subq_12 + ) subq_13 + ) subq_14 GROUP BY - subq_13.metric_time__day - ) subq_14 + subq_14.metric_time__day + ) subq_15 ON - subq_4.metric_time__day = subq_14.metric_time__day + subq_4.metric_time__day = subq_15.metric_time__day GROUP BY - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) -) subq_15 + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) +) subq_16 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_window__plan0_optimized.sql index 94618c5f72..cf65b76be7 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_window__plan0_optimized.sql @@ -5,9 +5,9 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_20.visits) AS visits - , MAX(subq_30.buys) AS buys + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , MAX(subq_21.visits) AS visits + , MAX(subq_32.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -22,11 +22,11 @@ FROM ( DATE_TRUNC('day', ds) AS metric_time__day , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_18 + ) subq_19 WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__day - ) subq_20 + ) subq_21 FULL OUTER JOIN ( -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'metric_time__day'] @@ -37,51 +37,60 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_23.visits) OVER ( + FIRST_VALUE(subq_25.visits) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_23.ds__day) OVER ( + , FIRST_VALUE(subq_25.ds__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_23.metric_time__day) OVER ( + , FIRST_VALUE(subq_25.metric_time__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_23.user) OVER ( + , FIRST_VALUE(subq_25.user) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_26.mf_internal_uuid AS mf_internal_uuid - , subq_26.buys AS buys + , subq_28.mf_internal_uuid AS mf_internal_uuid + , subq_28.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + ds__day + , metric_time__day + , subq_23.user + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_23 + WHERE metric_time__day = '2020-01-01' + ) subq_25 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -92,23 +101,23 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_26 + ) subq_28 ON ( - subq_23.user = subq_26.user + subq_25.user = subq_28.user ) AND ( ( - subq_23.ds__day <= subq_26.ds__day + subq_25.ds__day <= subq_28.ds__day ) AND ( - subq_23.ds__day > subq_26.ds__day - MAKE_INTERVAL(days => 7) + subq_25.ds__day > subq_28.ds__day - MAKE_INTERVAL(days => 7) ) ) - ) subq_27 + ) subq_29 GROUP BY metric_time__day - ) subq_30 + ) subq_32 ON - subq_20.metric_time__day = subq_30.metric_time__day + subq_21.metric_time__day = subq_32.metric_time__day GROUP BY - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) -) subq_31 + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) +) subq_33 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_window_and_time_constraint__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_window_and_time_constraint__plan0.sql index 95f5b33f4c..e6d463aacd 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_window_and_time_constraint__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_window_and_time_constraint__plan0.sql @@ -1,15 +1,15 @@ -- Compute Metrics via Expressions SELECT - subq_17.metric_time__day - , subq_17.visit__referrer_id - , CAST(subq_17.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_17.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_7days + subq_18.metric_time__day + , subq_18.visit__referrer_id + , CAST(subq_18.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_18.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_5.metric_time__day, subq_16.metric_time__day) AS metric_time__day - , COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_5.metric_time__day, subq_17.metric_time__day) AS metric_time__day + , COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) AS visit__referrer_id , MAX(subq_5.visits) AS visits - , MAX(subq_16.buys) AS buys + , MAX(subq_17.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -164,289 +164,335 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_15.metric_time__day - , subq_15.visit__referrer_id - , SUM(subq_15.buys) AS buys + subq_16.metric_time__day + , subq_16.visit__referrer_id + , SUM(subq_16.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] SELECT - subq_14.metric_time__day - , subq_14.visit__referrer_id - , subq_14.buys + subq_15.metric_time__day + , subq_15.visit__referrer_id + , subq_15.buys FROM ( -- Find conversions for user within the range of 7 day SELECT - subq_13.ds__day - , subq_13.metric_time__day - , subq_13.user - , subq_13.visit__referrer_id - , subq_13.buys - , subq_13.visits + subq_14.ds__day + , subq_14.metric_time__day + , subq_14.user + , subq_14.visit__referrer_id + , subq_14.buys + , subq_14.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_9.visits) OVER ( + FIRST_VALUE(subq_10.visits) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_9.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_10.visit__referrer_id) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_9.ds__day) OVER ( + , FIRST_VALUE(subq_10.ds__day) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_9.metric_time__day) OVER ( + , FIRST_VALUE(subq_10.metric_time__day) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_9.user) OVER ( + , FIRST_VALUE(subq_10.user) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_12.mf_internal_uuid AS mf_internal_uuid - , subq_12.buys AS buys + , subq_13.mf_internal_uuid AS mf_internal_uuid + , subq_13.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_8.ds__day - , subq_8.metric_time__day - , subq_8.user - , subq_8.visit__referrer_id - , subq_8.visits + subq_9.ds__day + , subq_9.metric_time__day + , subq_9.user + , subq_9.visit__referrer_id + , subq_9.visits FROM ( - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE SELECT - subq_7.ds__day - , subq_7.ds__week - , subq_7.ds__month - , subq_7.ds__quarter - , subq_7.ds__year - , subq_7.ds__extract_year - , subq_7.ds__extract_quarter - , subq_7.ds__extract_month - , subq_7.ds__extract_day - , subq_7.ds__extract_dow - , subq_7.ds__extract_doy - , subq_7.visit__ds__day - , subq_7.visit__ds__week - , subq_7.visit__ds__month - , subq_7.visit__ds__quarter - , subq_7.visit__ds__year - , subq_7.visit__ds__extract_year - , subq_7.visit__ds__extract_quarter - , subq_7.visit__ds__extract_month - , subq_7.visit__ds__extract_day - , subq_7.visit__ds__extract_dow - , subq_7.visit__ds__extract_doy - , subq_7.metric_time__day - , subq_7.metric_time__week - , subq_7.metric_time__month - , subq_7.metric_time__quarter - , subq_7.metric_time__year - , subq_7.metric_time__extract_year - , subq_7.metric_time__extract_quarter - , subq_7.metric_time__extract_month - , subq_7.metric_time__extract_day - , subq_7.metric_time__extract_dow - , subq_7.metric_time__extract_doy - , subq_7.user - , subq_7.session - , subq_7.visit__user - , subq_7.visit__session - , subq_7.referrer_id - , subq_7.visit__referrer_id - , subq_7.visits - , subq_7.visitors + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.visit__ds__day + , subq_8.visit__ds__week + , subq_8.visit__ds__month + , subq_8.visit__ds__quarter + , subq_8.visit__ds__year + , subq_8.visit__ds__extract_year + , subq_8.visit__ds__extract_quarter + , subq_8.visit__ds__extract_month + , subq_8.visit__ds__extract_day + , subq_8.visit__ds__extract_dow + , subq_8.visit__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session + , subq_8.visit__user + , subq_8.visit__session + , subq_8.referrer_id + , subq_8.visit__referrer_id + , subq_8.visits + , subq_8.visitors FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] SELECT - subq_6.ds__day - , subq_6.ds__week - , subq_6.ds__month - , subq_6.ds__quarter - , subq_6.ds__year - , subq_6.ds__extract_year - , subq_6.ds__extract_quarter - , subq_6.ds__extract_month - , subq_6.ds__extract_day - , subq_6.ds__extract_dow - , subq_6.ds__extract_doy - , subq_6.visit__ds__day - , subq_6.visit__ds__week - , subq_6.visit__ds__month - , subq_6.visit__ds__quarter - , subq_6.visit__ds__year - , subq_6.visit__ds__extract_year - , subq_6.visit__ds__extract_quarter - , subq_6.visit__ds__extract_month - , subq_6.visit__ds__extract_day - , subq_6.visit__ds__extract_dow - , subq_6.visit__ds__extract_doy - , subq_6.ds__day AS metric_time__day - , subq_6.ds__week AS metric_time__week - , subq_6.ds__month AS metric_time__month - , subq_6.ds__quarter AS metric_time__quarter - , subq_6.ds__year AS metric_time__year - , subq_6.ds__extract_year AS metric_time__extract_year - , subq_6.ds__extract_quarter AS metric_time__extract_quarter - , subq_6.ds__extract_month AS metric_time__extract_month - , subq_6.ds__extract_day AS metric_time__extract_day - , subq_6.ds__extract_dow AS metric_time__extract_dow - , subq_6.ds__extract_doy AS metric_time__extract_doy - , subq_6.user - , subq_6.session - , subq_6.visit__user - , subq_6.visit__session - , subq_6.referrer_id - , subq_6.visit__referrer_id - , subq_6.visits - , subq_6.visitors + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_6 - ) subq_7 - WHERE subq_7.metric_time__day BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_8 - ) subq_9 + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE subq_7.metric_time__day BETWEEN '2020-01-01' AND '2020-01-02' + ) subq_8 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_9 + ) subq_10 INNER JOIN ( -- Add column with generated UUID SELECT - subq_11.ds__day - , subq_11.ds__week - , subq_11.ds__month - , subq_11.ds__quarter - , subq_11.ds__year - , subq_11.ds__extract_year - , subq_11.ds__extract_quarter - , subq_11.ds__extract_month - , subq_11.ds__extract_day - , subq_11.ds__extract_dow - , subq_11.ds__extract_doy - , subq_11.buy__ds__day - , subq_11.buy__ds__week - , subq_11.buy__ds__month - , subq_11.buy__ds__quarter - , subq_11.buy__ds__year - , subq_11.buy__ds__extract_year - , subq_11.buy__ds__extract_quarter - , subq_11.buy__ds__extract_month - , subq_11.buy__ds__extract_day - , subq_11.buy__ds__extract_dow - , subq_11.buy__ds__extract_doy - , subq_11.metric_time__day - , subq_11.metric_time__week - , subq_11.metric_time__month - , subq_11.metric_time__quarter - , subq_11.metric_time__year - , subq_11.metric_time__extract_year - , subq_11.metric_time__extract_quarter - , subq_11.metric_time__extract_month - , subq_11.metric_time__extract_day - , subq_11.metric_time__extract_dow - , subq_11.metric_time__extract_doy - , subq_11.user - , subq_11.session_id - , subq_11.buy__user - , subq_11.buy__session_id - , subq_11.buys - , subq_11.buyers + subq_12.ds__day + , subq_12.ds__week + , subq_12.ds__month + , subq_12.ds__quarter + , subq_12.ds__year + , subq_12.ds__extract_year + , subq_12.ds__extract_quarter + , subq_12.ds__extract_month + , subq_12.ds__extract_day + , subq_12.ds__extract_dow + , subq_12.ds__extract_doy + , subq_12.buy__ds__day + , subq_12.buy__ds__week + , subq_12.buy__ds__month + , subq_12.buy__ds__quarter + , subq_12.buy__ds__year + , subq_12.buy__ds__extract_year + , subq_12.buy__ds__extract_quarter + , subq_12.buy__ds__extract_month + , subq_12.buy__ds__extract_day + , subq_12.buy__ds__extract_dow + , subq_12.buy__ds__extract_doy + , subq_12.metric_time__day + , subq_12.metric_time__week + , subq_12.metric_time__month + , subq_12.metric_time__quarter + , subq_12.metric_time__year + , subq_12.metric_time__extract_year + , subq_12.metric_time__extract_quarter + , subq_12.metric_time__extract_month + , subq_12.metric_time__extract_day + , subq_12.metric_time__extract_dow + , subq_12.metric_time__extract_doy + , subq_12.user + , subq_12.session_id + , subq_12.buy__user + , subq_12.buy__session_id + , subq_12.buys + , subq_12.buyers , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_10.ds__day - , subq_10.ds__week - , subq_10.ds__month - , subq_10.ds__quarter - , subq_10.ds__year - , subq_10.ds__extract_year - , subq_10.ds__extract_quarter - , subq_10.ds__extract_month - , subq_10.ds__extract_day - , subq_10.ds__extract_dow - , subq_10.ds__extract_doy - , subq_10.buy__ds__day - , subq_10.buy__ds__week - , subq_10.buy__ds__month - , subq_10.buy__ds__quarter - , subq_10.buy__ds__year - , subq_10.buy__ds__extract_year - , subq_10.buy__ds__extract_quarter - , subq_10.buy__ds__extract_month - , subq_10.buy__ds__extract_day - , subq_10.buy__ds__extract_dow - , subq_10.buy__ds__extract_doy - , subq_10.ds__day AS metric_time__day - , subq_10.ds__week AS metric_time__week - , subq_10.ds__month AS metric_time__month - , subq_10.ds__quarter AS metric_time__quarter - , subq_10.ds__year AS metric_time__year - , subq_10.ds__extract_year AS metric_time__extract_year - , subq_10.ds__extract_quarter AS metric_time__extract_quarter - , subq_10.ds__extract_month AS metric_time__extract_month - , subq_10.ds__extract_day AS metric_time__extract_day - , subq_10.ds__extract_dow AS metric_time__extract_dow - , subq_10.ds__extract_doy AS metric_time__extract_doy - , subq_10.user - , subq_10.session_id - , subq_10.buy__user - , subq_10.buy__session_id - , subq_10.buys - , subq_10.buyers + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.ds__day AS metric_time__day + , subq_11.ds__week AS metric_time__week + , subq_11.ds__month AS metric_time__month + , subq_11.ds__quarter AS metric_time__quarter + , subq_11.ds__year AS metric_time__year + , subq_11.ds__extract_year AS metric_time__extract_year + , subq_11.ds__extract_quarter AS metric_time__extract_quarter + , subq_11.ds__extract_month AS metric_time__extract_month + , subq_11.ds__extract_day AS metric_time__extract_day + , subq_11.ds__extract_dow AS metric_time__extract_dow + , subq_11.ds__extract_doy AS metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -479,33 +525,33 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_10 - ) subq_11 - ) subq_12 + ) subq_11 + ) subq_12 + ) subq_13 ON ( - subq_9.user = subq_12.user + subq_10.user = subq_13.user ) AND ( ( - subq_9.ds__day <= subq_12.ds__day + subq_10.ds__day <= subq_13.ds__day ) AND ( - subq_9.ds__day > subq_12.ds__day - MAKE_INTERVAL(days => 7) + subq_10.ds__day > subq_13.ds__day - MAKE_INTERVAL(days => 7) ) ) - ) subq_13 - ) subq_14 - ) subq_15 + ) subq_14 + ) subq_15 + ) subq_16 GROUP BY - subq_15.metric_time__day - , subq_15.visit__referrer_id - ) subq_16 + subq_16.metric_time__day + , subq_16.visit__referrer_id + ) subq_17 ON ( - subq_5.visit__referrer_id = subq_16.visit__referrer_id + subq_5.visit__referrer_id = subq_17.visit__referrer_id ) AND ( - subq_5.metric_time__day = subq_16.metric_time__day + subq_5.metric_time__day = subq_17.metric_time__day ) GROUP BY - COALESCE(subq_5.metric_time__day, subq_16.metric_time__day) - , COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) -) subq_17 + COALESCE(subq_5.metric_time__day, subq_17.metric_time__day) + , COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) +) subq_18 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql index ab787834c1..1f151978c9 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql @@ -6,10 +6,10 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_23.metric_time__day, subq_34.metric_time__day) AS metric_time__day - , COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) AS visit__referrer_id - , MAX(subq_23.visits) AS visits - , MAX(subq_34.buys) AS buys + COALESCE(subq_24.metric_time__day, subq_36.metric_time__day) AS metric_time__day + , COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) AS visit__referrer_id + , MAX(subq_24.visits) AS visits + , MAX(subq_36.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -28,12 +28,12 @@ FROM ( , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_21 + ) subq_22 WHERE visit__referrer_id = 'ref_id_01' GROUP BY metric_time__day , visit__referrer_id - ) subq_23 + ) subq_24 FULL OUTER JOIN ( -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] @@ -45,62 +45,72 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_27.visits) OVER ( + FIRST_VALUE(subq_29.visits) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_27.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_29.visit__referrer_id) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_27.ds__day) OVER ( + , FIRST_VALUE(subq_29.ds__day) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_27.metric_time__day) OVER ( + , FIRST_VALUE(subq_29.metric_time__day) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_27.user) OVER ( + , FIRST_VALUE(subq_29.user) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_30.mf_internal_uuid AS mf_internal_uuid - , subq_30.buys AS buys + , subq_32.mf_internal_uuid AS mf_internal_uuid + , subq_32.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_27 + ds__day + , metric_time__day + , subq_27.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' + ) subq_27 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_29 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -111,29 +121,29 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_30 + ) subq_32 ON ( - subq_27.user = subq_30.user + subq_29.user = subq_32.user ) AND ( ( - subq_27.ds__day <= subq_30.ds__day + subq_29.ds__day <= subq_32.ds__day ) AND ( - subq_27.ds__day > subq_30.ds__day - MAKE_INTERVAL(days => 7) + subq_29.ds__day > subq_32.ds__day - MAKE_INTERVAL(days => 7) ) ) - ) subq_31 + ) subq_33 GROUP BY metric_time__day , visit__referrer_id - ) subq_34 + ) subq_36 ON ( - subq_23.visit__referrer_id = subq_34.visit__referrer_id + subq_24.visit__referrer_id = subq_36.visit__referrer_id ) AND ( - subq_23.metric_time__day = subq_34.metric_time__day + subq_24.metric_time__day = subq_36.metric_time__day ) GROUP BY - COALESCE(subq_23.metric_time__day, subq_34.metric_time__day) - , COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) -) subq_35 + COALESCE(subq_24.metric_time__day, subq_36.metric_time__day) + , COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) +) subq_37 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric__plan0.sql index 2c457d1da7..06cfe21961 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric__plan0.sql @@ -1,13 +1,13 @@ -- Compute Metrics via Expressions SELECT - subq_15.metric_time__day - , CAST(subq_15.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_15.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate + subq_16.metric_time__day + , CAST(subq_16.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_16.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) AS metric_time__day + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) AS metric_time__day , MAX(subq_4.visits) AS visits - , MAX(subq_14.buys) AS buys + , MAX(subq_15.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -112,231 +112,277 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_13.metric_time__day - , SUM(subq_13.buys) AS buys + subq_14.metric_time__day + , SUM(subq_14.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'metric_time__day'] SELECT - subq_12.metric_time__day - , subq_12.buys + subq_13.metric_time__day + , subq_13.buys FROM ( -- Find conversions for user within the range of INF SELECT - subq_11.ds__day - , subq_11.metric_time__day - , subq_11.user - , subq_11.buys - , subq_11.visits + subq_12.ds__day + , subq_12.metric_time__day + , subq_12.user + , subq_12.buys + , subq_12.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_7.visits) OVER ( + FIRST_VALUE(subq_8.visits) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_7.ds__day) OVER ( + , FIRST_VALUE(subq_8.ds__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_7.metric_time__day) OVER ( + , FIRST_VALUE(subq_8.metric_time__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_7.user) OVER ( + , FIRST_VALUE(subq_8.user) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_10.mf_internal_uuid AS mf_internal_uuid - , subq_10.buys AS buys + , subq_11.mf_internal_uuid AS mf_internal_uuid + , subq_11.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_6.ds__day - , subq_6.metric_time__day - , subq_6.user - , subq_6.visits + subq_7.ds__day + , subq_7.metric_time__day + , subq_7.user + , subq_7.visits FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE SELECT - subq_5.ds__day - , subq_5.ds__week - , subq_5.ds__month - , subq_5.ds__quarter - , subq_5.ds__year - , subq_5.ds__extract_year - , subq_5.ds__extract_quarter - , subq_5.ds__extract_month - , subq_5.ds__extract_day - , subq_5.ds__extract_dow - , subq_5.ds__extract_doy - , subq_5.visit__ds__day - , subq_5.visit__ds__week - , subq_5.visit__ds__month - , subq_5.visit__ds__quarter - , subq_5.visit__ds__year - , subq_5.visit__ds__extract_year - , subq_5.visit__ds__extract_quarter - , subq_5.visit__ds__extract_month - , subq_5.visit__ds__extract_day - , subq_5.visit__ds__extract_dow - , subq_5.visit__ds__extract_doy - , subq_5.ds__day AS metric_time__day - , subq_5.ds__week AS metric_time__week - , subq_5.ds__month AS metric_time__month - , subq_5.ds__quarter AS metric_time__quarter - , subq_5.ds__year AS metric_time__year - , subq_5.ds__extract_year AS metric_time__extract_year - , subq_5.ds__extract_quarter AS metric_time__extract_quarter - , subq_5.ds__extract_month AS metric_time__extract_month - , subq_5.ds__extract_day AS metric_time__extract_day - , subq_5.ds__extract_dow AS metric_time__extract_dow - , subq_5.ds__extract_doy AS metric_time__extract_doy - , subq_5.user - , subq_5.session - , subq_5.visit__user - , subq_5.visit__session - , subq_5.referrer_id - , subq_5.visit__referrer_id - , subq_5.visits - , subq_5.visitors + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.metric_time__day + , subq_6.metric_time__week + , subq_6.metric_time__month + , subq_6.metric_time__quarter + , subq_6.metric_time__year + , subq_6.metric_time__extract_year + , subq_6.metric_time__extract_quarter + , subq_6.metric_time__extract_month + , subq_6.metric_time__extract_day + , subq_6.metric_time__extract_dow + , subq_6.metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_5 - ) subq_6 - ) subq_7 + subq_5.ds__day + , subq_5.ds__week + , subq_5.ds__month + , subq_5.ds__quarter + , subq_5.ds__year + , subq_5.ds__extract_year + , subq_5.ds__extract_quarter + , subq_5.ds__extract_month + , subq_5.ds__extract_day + , subq_5.ds__extract_dow + , subq_5.ds__extract_doy + , subq_5.visit__ds__day + , subq_5.visit__ds__week + , subq_5.visit__ds__month + , subq_5.visit__ds__quarter + , subq_5.visit__ds__year + , subq_5.visit__ds__extract_year + , subq_5.visit__ds__extract_quarter + , subq_5.visit__ds__extract_month + , subq_5.visit__ds__extract_day + , subq_5.visit__ds__extract_dow + , subq_5.visit__ds__extract_doy + , subq_5.ds__day AS metric_time__day + , subq_5.ds__week AS metric_time__week + , subq_5.ds__month AS metric_time__month + , subq_5.ds__quarter AS metric_time__quarter + , subq_5.ds__year AS metric_time__year + , subq_5.ds__extract_year AS metric_time__extract_year + , subq_5.ds__extract_quarter AS metric_time__extract_quarter + , subq_5.ds__extract_month AS metric_time__extract_month + , subq_5.ds__extract_day AS metric_time__extract_day + , subq_5.ds__extract_dow AS metric_time__extract_dow + , subq_5.ds__extract_doy AS metric_time__extract_doy + , subq_5.user + , subq_5.session + , subq_5.visit__user + , subq_5.visit__session + , subq_5.referrer_id + , subq_5.visit__referrer_id + , subq_5.visits + , subq_5.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_5 + ) subq_6 + WHERE metric_time__day = '2020-01-01' + ) subq_7 + ) subq_8 INNER JOIN ( -- Add column with generated UUID SELECT - subq_9.ds__day - , subq_9.ds__week - , subq_9.ds__month - , subq_9.ds__quarter - , subq_9.ds__year - , subq_9.ds__extract_year - , subq_9.ds__extract_quarter - , subq_9.ds__extract_month - , subq_9.ds__extract_day - , subq_9.ds__extract_dow - , subq_9.ds__extract_doy - , subq_9.buy__ds__day - , subq_9.buy__ds__week - , subq_9.buy__ds__month - , subq_9.buy__ds__quarter - , subq_9.buy__ds__year - , subq_9.buy__ds__extract_year - , subq_9.buy__ds__extract_quarter - , subq_9.buy__ds__extract_month - , subq_9.buy__ds__extract_day - , subq_9.buy__ds__extract_dow - , subq_9.buy__ds__extract_doy - , subq_9.metric_time__day - , subq_9.metric_time__week - , subq_9.metric_time__month - , subq_9.metric_time__quarter - , subq_9.metric_time__year - , subq_9.metric_time__extract_year - , subq_9.metric_time__extract_quarter - , subq_9.metric_time__extract_month - , subq_9.metric_time__extract_day - , subq_9.metric_time__extract_dow - , subq_9.metric_time__extract_doy - , subq_9.user - , subq_9.session_id - , subq_9.buy__user - , subq_9.buy__session_id - , subq_9.buys - , subq_9.buyers + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.metric_time__day + , subq_10.metric_time__week + , subq_10.metric_time__month + , subq_10.metric_time__quarter + , subq_10.metric_time__year + , subq_10.metric_time__extract_year + , subq_10.metric_time__extract_quarter + , subq_10.metric_time__extract_month + , subq_10.metric_time__extract_day + , subq_10.metric_time__extract_dow + , subq_10.metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_8.ds__day - , subq_8.ds__week - , subq_8.ds__month - , subq_8.ds__quarter - , subq_8.ds__year - , subq_8.ds__extract_year - , subq_8.ds__extract_quarter - , subq_8.ds__extract_month - , subq_8.ds__extract_day - , subq_8.ds__extract_dow - , subq_8.ds__extract_doy - , subq_8.buy__ds__day - , subq_8.buy__ds__week - , subq_8.buy__ds__month - , subq_8.buy__ds__quarter - , subq_8.buy__ds__year - , subq_8.buy__ds__extract_year - , subq_8.buy__ds__extract_quarter - , subq_8.buy__ds__extract_month - , subq_8.buy__ds__extract_day - , subq_8.buy__ds__extract_dow - , subq_8.buy__ds__extract_doy - , subq_8.ds__day AS metric_time__day - , subq_8.ds__week AS metric_time__week - , subq_8.ds__month AS metric_time__month - , subq_8.ds__quarter AS metric_time__quarter - , subq_8.ds__year AS metric_time__year - , subq_8.ds__extract_year AS metric_time__extract_year - , subq_8.ds__extract_quarter AS metric_time__extract_quarter - , subq_8.ds__extract_month AS metric_time__extract_month - , subq_8.ds__extract_day AS metric_time__extract_day - , subq_8.ds__extract_dow AS metric_time__extract_dow - , subq_8.ds__extract_doy AS metric_time__extract_doy - , subq_8.user - , subq_8.session_id - , subq_8.buy__user - , subq_8.buy__session_id - , subq_8.buys - , subq_8.buyers + subq_9.ds__day + , subq_9.ds__week + , subq_9.ds__month + , subq_9.ds__quarter + , subq_9.ds__year + , subq_9.ds__extract_year + , subq_9.ds__extract_quarter + , subq_9.ds__extract_month + , subq_9.ds__extract_day + , subq_9.ds__extract_dow + , subq_9.ds__extract_doy + , subq_9.buy__ds__day + , subq_9.buy__ds__week + , subq_9.buy__ds__month + , subq_9.buy__ds__quarter + , subq_9.buy__ds__year + , subq_9.buy__ds__extract_year + , subq_9.buy__ds__extract_quarter + , subq_9.buy__ds__extract_month + , subq_9.buy__ds__extract_day + , subq_9.buy__ds__extract_dow + , subq_9.buy__ds__extract_doy + , subq_9.ds__day AS metric_time__day + , subq_9.ds__week AS metric_time__week + , subq_9.ds__month AS metric_time__month + , subq_9.ds__quarter AS metric_time__quarter + , subq_9.ds__year AS metric_time__year + , subq_9.ds__extract_year AS metric_time__extract_year + , subq_9.ds__extract_quarter AS metric_time__extract_quarter + , subq_9.ds__extract_month AS metric_time__extract_month + , subq_9.ds__extract_day AS metric_time__extract_day + , subq_9.ds__extract_dow AS metric_time__extract_dow + , subq_9.ds__extract_doy AS metric_time__extract_doy + , subq_9.user + , subq_9.session_id + , subq_9.buy__user + , subq_9.buy__session_id + , subq_9.buys + , subq_9.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -369,23 +415,23 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_8 - ) subq_9 - ) subq_10 + ) subq_9 + ) subq_10 + ) subq_11 ON ( - subq_7.user = subq_10.user + subq_8.user = subq_11.user ) AND ( - (subq_7.ds__day <= subq_10.ds__day) + (subq_8.ds__day <= subq_11.ds__day) ) - ) subq_11 - ) subq_12 - ) subq_13 + ) subq_12 + ) subq_13 + ) subq_14 GROUP BY - subq_13.metric_time__day - ) subq_14 + subq_14.metric_time__day + ) subq_15 ON - subq_4.metric_time__day = subq_14.metric_time__day + subq_4.metric_time__day = subq_15.metric_time__day GROUP BY - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) -) subq_15 + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) +) subq_16 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric__plan0_optimized.sql index 06f8ca6cc6..b2d2c05860 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric__plan0_optimized.sql @@ -5,9 +5,9 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_20.visits) AS visits - , MAX(subq_30.buys) AS buys + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , MAX(subq_21.visits) AS visits + , MAX(subq_32.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -22,11 +22,11 @@ FROM ( DATE_TRUNC('day', ds) AS metric_time__day , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_18 + ) subq_19 WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__day - ) subq_20 + ) subq_21 FULL OUTER JOIN ( -- Find conversions for user within the range of INF -- Pass Only Elements: ['buys', 'metric_time__day'] @@ -37,51 +37,60 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_23.visits) OVER ( + FIRST_VALUE(subq_25.visits) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_23.ds__day) OVER ( + , FIRST_VALUE(subq_25.ds__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_23.metric_time__day) OVER ( + , FIRST_VALUE(subq_25.metric_time__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_23.user) OVER ( + , FIRST_VALUE(subq_25.user) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_26.mf_internal_uuid AS mf_internal_uuid - , subq_26.buys AS buys + , subq_28.mf_internal_uuid AS mf_internal_uuid + , subq_28.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + ds__day + , metric_time__day + , subq_23.user + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_23 + WHERE metric_time__day = '2020-01-01' + ) subq_25 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -92,19 +101,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_26 + ) subq_28 ON ( - subq_23.user = subq_26.user + subq_25.user = subq_28.user ) AND ( - (subq_23.ds__day <= subq_26.ds__day) + (subq_25.ds__day <= subq_28.ds__day) ) - ) subq_27 + ) subq_29 GROUP BY metric_time__day - ) subq_30 + ) subq_32 ON - subq_20.metric_time__day = subq_30.metric_time__day + subq_21.metric_time__day = subq_32.metric_time__day GROUP BY - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) -) subq_31 + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) +) subq_33 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_categorical_filter__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_categorical_filter__plan0.sql index 6497c3264f..082aa9c264 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_categorical_filter__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_categorical_filter__plan0.sql @@ -1,15 +1,15 @@ -- Compute Metrics via Expressions SELECT - subq_15.metric_time__day - , subq_15.visit__referrer_id - , CAST(subq_15.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_15.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate + subq_16.metric_time__day + , subq_16.visit__referrer_id + , CAST(subq_16.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_16.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) AS metric_time__day - , COALESCE(subq_4.visit__referrer_id, subq_14.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) AS metric_time__day + , COALESCE(subq_4.visit__referrer_id, subq_15.visit__referrer_id) AS visit__referrer_id , MAX(subq_4.visits) AS visits - , MAX(subq_14.buys) AS buys + , MAX(subq_15.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -118,243 +118,289 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_13.metric_time__day - , subq_13.visit__referrer_id - , SUM(subq_13.buys) AS buys + subq_14.metric_time__day + , subq_14.visit__referrer_id + , SUM(subq_14.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] SELECT - subq_12.metric_time__day - , subq_12.visit__referrer_id - , subq_12.buys + subq_13.metric_time__day + , subq_13.visit__referrer_id + , subq_13.buys FROM ( -- Find conversions for user within the range of INF SELECT - subq_11.ds__day - , subq_11.metric_time__day - , subq_11.user - , subq_11.visit__referrer_id - , subq_11.buys - , subq_11.visits + subq_12.ds__day + , subq_12.metric_time__day + , subq_12.user + , subq_12.visit__referrer_id + , subq_12.buys + , subq_12.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_7.visits) OVER ( + FIRST_VALUE(subq_8.visits) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_7.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_8.visit__referrer_id) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_7.ds__day) OVER ( + , FIRST_VALUE(subq_8.ds__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_7.metric_time__day) OVER ( + , FIRST_VALUE(subq_8.metric_time__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_7.user) OVER ( + , FIRST_VALUE(subq_8.user) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_10.mf_internal_uuid AS mf_internal_uuid - , subq_10.buys AS buys + , subq_11.mf_internal_uuid AS mf_internal_uuid + , subq_11.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_6.ds__day - , subq_6.metric_time__day - , subq_6.user - , subq_6.visit__referrer_id - , subq_6.visits + subq_7.ds__day + , subq_7.metric_time__day + , subq_7.user + , subq_7.visit__referrer_id + , subq_7.visits FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE SELECT - subq_5.ds__day - , subq_5.ds__week - , subq_5.ds__month - , subq_5.ds__quarter - , subq_5.ds__year - , subq_5.ds__extract_year - , subq_5.ds__extract_quarter - , subq_5.ds__extract_month - , subq_5.ds__extract_day - , subq_5.ds__extract_dow - , subq_5.ds__extract_doy - , subq_5.visit__ds__day - , subq_5.visit__ds__week - , subq_5.visit__ds__month - , subq_5.visit__ds__quarter - , subq_5.visit__ds__year - , subq_5.visit__ds__extract_year - , subq_5.visit__ds__extract_quarter - , subq_5.visit__ds__extract_month - , subq_5.visit__ds__extract_day - , subq_5.visit__ds__extract_dow - , subq_5.visit__ds__extract_doy - , subq_5.ds__day AS metric_time__day - , subq_5.ds__week AS metric_time__week - , subq_5.ds__month AS metric_time__month - , subq_5.ds__quarter AS metric_time__quarter - , subq_5.ds__year AS metric_time__year - , subq_5.ds__extract_year AS metric_time__extract_year - , subq_5.ds__extract_quarter AS metric_time__extract_quarter - , subq_5.ds__extract_month AS metric_time__extract_month - , subq_5.ds__extract_day AS metric_time__extract_day - , subq_5.ds__extract_dow AS metric_time__extract_dow - , subq_5.ds__extract_doy AS metric_time__extract_doy - , subq_5.user - , subq_5.session - , subq_5.visit__user - , subq_5.visit__session - , subq_5.referrer_id - , subq_5.visit__referrer_id - , subq_5.visits - , subq_5.visitors + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.metric_time__day + , subq_6.metric_time__week + , subq_6.metric_time__month + , subq_6.metric_time__quarter + , subq_6.metric_time__year + , subq_6.metric_time__extract_year + , subq_6.metric_time__extract_quarter + , subq_6.metric_time__extract_month + , subq_6.metric_time__extract_day + , subq_6.metric_time__extract_dow + , subq_6.metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_5 - ) subq_6 - ) subq_7 + subq_5.ds__day + , subq_5.ds__week + , subq_5.ds__month + , subq_5.ds__quarter + , subq_5.ds__year + , subq_5.ds__extract_year + , subq_5.ds__extract_quarter + , subq_5.ds__extract_month + , subq_5.ds__extract_day + , subq_5.ds__extract_dow + , subq_5.ds__extract_doy + , subq_5.visit__ds__day + , subq_5.visit__ds__week + , subq_5.visit__ds__month + , subq_5.visit__ds__quarter + , subq_5.visit__ds__year + , subq_5.visit__ds__extract_year + , subq_5.visit__ds__extract_quarter + , subq_5.visit__ds__extract_month + , subq_5.visit__ds__extract_day + , subq_5.visit__ds__extract_dow + , subq_5.visit__ds__extract_doy + , subq_5.ds__day AS metric_time__day + , subq_5.ds__week AS metric_time__week + , subq_5.ds__month AS metric_time__month + , subq_5.ds__quarter AS metric_time__quarter + , subq_5.ds__year AS metric_time__year + , subq_5.ds__extract_year AS metric_time__extract_year + , subq_5.ds__extract_quarter AS metric_time__extract_quarter + , subq_5.ds__extract_month AS metric_time__extract_month + , subq_5.ds__extract_day AS metric_time__extract_day + , subq_5.ds__extract_dow AS metric_time__extract_dow + , subq_5.ds__extract_doy AS metric_time__extract_doy + , subq_5.user + , subq_5.session + , subq_5.visit__user + , subq_5.visit__session + , subq_5.referrer_id + , subq_5.visit__referrer_id + , subq_5.visits + , subq_5.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_5 + ) subq_6 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_7 + ) subq_8 INNER JOIN ( -- Add column with generated UUID SELECT - subq_9.ds__day - , subq_9.ds__week - , subq_9.ds__month - , subq_9.ds__quarter - , subq_9.ds__year - , subq_9.ds__extract_year - , subq_9.ds__extract_quarter - , subq_9.ds__extract_month - , subq_9.ds__extract_day - , subq_9.ds__extract_dow - , subq_9.ds__extract_doy - , subq_9.buy__ds__day - , subq_9.buy__ds__week - , subq_9.buy__ds__month - , subq_9.buy__ds__quarter - , subq_9.buy__ds__year - , subq_9.buy__ds__extract_year - , subq_9.buy__ds__extract_quarter - , subq_9.buy__ds__extract_month - , subq_9.buy__ds__extract_day - , subq_9.buy__ds__extract_dow - , subq_9.buy__ds__extract_doy - , subq_9.metric_time__day - , subq_9.metric_time__week - , subq_9.metric_time__month - , subq_9.metric_time__quarter - , subq_9.metric_time__year - , subq_9.metric_time__extract_year - , subq_9.metric_time__extract_quarter - , subq_9.metric_time__extract_month - , subq_9.metric_time__extract_day - , subq_9.metric_time__extract_dow - , subq_9.metric_time__extract_doy - , subq_9.user - , subq_9.session_id - , subq_9.buy__user - , subq_9.buy__session_id - , subq_9.buys - , subq_9.buyers + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.metric_time__day + , subq_10.metric_time__week + , subq_10.metric_time__month + , subq_10.metric_time__quarter + , subq_10.metric_time__year + , subq_10.metric_time__extract_year + , subq_10.metric_time__extract_quarter + , subq_10.metric_time__extract_month + , subq_10.metric_time__extract_day + , subq_10.metric_time__extract_dow + , subq_10.metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_8.ds__day - , subq_8.ds__week - , subq_8.ds__month - , subq_8.ds__quarter - , subq_8.ds__year - , subq_8.ds__extract_year - , subq_8.ds__extract_quarter - , subq_8.ds__extract_month - , subq_8.ds__extract_day - , subq_8.ds__extract_dow - , subq_8.ds__extract_doy - , subq_8.buy__ds__day - , subq_8.buy__ds__week - , subq_8.buy__ds__month - , subq_8.buy__ds__quarter - , subq_8.buy__ds__year - , subq_8.buy__ds__extract_year - , subq_8.buy__ds__extract_quarter - , subq_8.buy__ds__extract_month - , subq_8.buy__ds__extract_day - , subq_8.buy__ds__extract_dow - , subq_8.buy__ds__extract_doy - , subq_8.ds__day AS metric_time__day - , subq_8.ds__week AS metric_time__week - , subq_8.ds__month AS metric_time__month - , subq_8.ds__quarter AS metric_time__quarter - , subq_8.ds__year AS metric_time__year - , subq_8.ds__extract_year AS metric_time__extract_year - , subq_8.ds__extract_quarter AS metric_time__extract_quarter - , subq_8.ds__extract_month AS metric_time__extract_month - , subq_8.ds__extract_day AS metric_time__extract_day - , subq_8.ds__extract_dow AS metric_time__extract_dow - , subq_8.ds__extract_doy AS metric_time__extract_doy - , subq_8.user - , subq_8.session_id - , subq_8.buy__user - , subq_8.buy__session_id - , subq_8.buys - , subq_8.buyers + subq_9.ds__day + , subq_9.ds__week + , subq_9.ds__month + , subq_9.ds__quarter + , subq_9.ds__year + , subq_9.ds__extract_year + , subq_9.ds__extract_quarter + , subq_9.ds__extract_month + , subq_9.ds__extract_day + , subq_9.ds__extract_dow + , subq_9.ds__extract_doy + , subq_9.buy__ds__day + , subq_9.buy__ds__week + , subq_9.buy__ds__month + , subq_9.buy__ds__quarter + , subq_9.buy__ds__year + , subq_9.buy__ds__extract_year + , subq_9.buy__ds__extract_quarter + , subq_9.buy__ds__extract_month + , subq_9.buy__ds__extract_day + , subq_9.buy__ds__extract_dow + , subq_9.buy__ds__extract_doy + , subq_9.ds__day AS metric_time__day + , subq_9.ds__week AS metric_time__week + , subq_9.ds__month AS metric_time__month + , subq_9.ds__quarter AS metric_time__quarter + , subq_9.ds__year AS metric_time__year + , subq_9.ds__extract_year AS metric_time__extract_year + , subq_9.ds__extract_quarter AS metric_time__extract_quarter + , subq_9.ds__extract_month AS metric_time__extract_month + , subq_9.ds__extract_day AS metric_time__extract_day + , subq_9.ds__extract_dow AS metric_time__extract_dow + , subq_9.ds__extract_doy AS metric_time__extract_doy + , subq_9.user + , subq_9.session_id + , subq_9.buy__user + , subq_9.buy__session_id + , subq_9.buys + , subq_9.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -387,29 +433,29 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_8 - ) subq_9 - ) subq_10 + ) subq_9 + ) subq_10 + ) subq_11 ON ( - subq_7.user = subq_10.user + subq_8.user = subq_11.user ) AND ( - (subq_7.ds__day <= subq_10.ds__day) + (subq_8.ds__day <= subq_11.ds__day) ) - ) subq_11 - ) subq_12 - ) subq_13 + ) subq_12 + ) subq_13 + ) subq_14 GROUP BY - subq_13.metric_time__day - , subq_13.visit__referrer_id - ) subq_14 + subq_14.metric_time__day + , subq_14.visit__referrer_id + ) subq_15 ON ( - subq_4.visit__referrer_id = subq_14.visit__referrer_id + subq_4.visit__referrer_id = subq_15.visit__referrer_id ) AND ( - subq_4.metric_time__day = subq_14.metric_time__day + subq_4.metric_time__day = subq_15.metric_time__day ) GROUP BY - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) - , COALESCE(subq_4.visit__referrer_id, subq_14.visit__referrer_id) -) subq_15 + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) + , COALESCE(subq_4.visit__referrer_id, subq_15.visit__referrer_id) +) subq_16 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_categorical_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_categorical_filter__plan0_optimized.sql index af0cf0df22..3757bbf5fe 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_categorical_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_categorical_filter__plan0_optimized.sql @@ -6,10 +6,10 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , COALESCE(subq_20.visit__referrer_id, subq_30.visit__referrer_id) AS visit__referrer_id - , MAX(subq_20.visits) AS visits - , MAX(subq_30.buys) AS buys + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , COALESCE(subq_21.visit__referrer_id, subq_32.visit__referrer_id) AS visit__referrer_id + , MAX(subq_21.visits) AS visits + , MAX(subq_32.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -26,12 +26,12 @@ FROM ( , referrer_id AS visit__referrer_id , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_18 + ) subq_19 WHERE visit__referrer_id = 'ref_id_01' GROUP BY metric_time__day , visit__referrer_id - ) subq_20 + ) subq_21 FULL OUTER JOIN ( -- Find conversions for user within the range of INF -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] @@ -43,60 +43,70 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_23.visits) OVER ( + FIRST_VALUE(subq_25.visits) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_23.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_25.visit__referrer_id) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_23.ds__day) OVER ( + , FIRST_VALUE(subq_25.ds__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_23.metric_time__day) OVER ( + , FIRST_VALUE(subq_25.metric_time__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_23.user) OVER ( + , FIRST_VALUE(subq_25.user) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_26.mf_internal_uuid AS mf_internal_uuid - , subq_26.buys AS buys + , subq_28.mf_internal_uuid AS mf_internal_uuid + , subq_28.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + ds__day + , metric_time__day + , subq_23.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_23 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_25 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -107,25 +117,25 @@ 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_26 + ) subq_28 ON ( - subq_23.user = subq_26.user + subq_25.user = subq_28.user ) AND ( - (subq_23.ds__day <= subq_26.ds__day) + (subq_25.ds__day <= subq_28.ds__day) ) - ) subq_27 + ) subq_29 GROUP BY metric_time__day , visit__referrer_id - ) subq_30 + ) subq_32 ON ( - subq_20.visit__referrer_id = subq_30.visit__referrer_id + subq_21.visit__referrer_id = subq_32.visit__referrer_id ) AND ( - subq_20.metric_time__day = subq_30.metric_time__day + subq_21.metric_time__day = subq_32.metric_time__day ) GROUP BY - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) - , COALESCE(subq_20.visit__referrer_id, subq_30.visit__referrer_id) -) subq_31 + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) + , COALESCE(subq_21.visit__referrer_id, subq_32.visit__referrer_id) +) subq_33 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_filter__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_filter__plan0.sql new file mode 100644 index 0000000000..0dedd4ef13 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_filter__plan0.sql @@ -0,0 +1,429 @@ +-- Compute Metrics via Expressions +SELECT + CAST(subq_17.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_17.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_5.visits) AS visits + , MAX(subq_16.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_4.visits) AS visits + FROM ( + -- Pass Only Elements: ['visits',] + SELECT + subq_3.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_2.metric_time__day + , subq_2.visits + FROM ( + -- Pass Only Elements: ['visits', 'metric_time__day'] + SELECT + subq_1.metric_time__day + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_0 + ) subq_1 + ) subq_2 + WHERE metric_time__day = '2020-01-01' + ) subq_3 + ) subq_4 + ) subq_5 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_15.buys) AS buys + FROM ( + -- Pass Only Elements: ['buys',] + SELECT + subq_14.buys + FROM ( + -- Find conversions for user within the range of INF + SELECT + subq_13.ds__day + , subq_13.metric_time__day + , subq_13.user + , subq_13.buys + , subq_13.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_9.visits) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_9.ds__day) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_9.metric_time__day) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS metric_time__day + , FIRST_VALUE(subq_9.user) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_12.mf_internal_uuid AS mf_internal_uuid + , subq_12.buys AS buys + FROM ( + -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] + SELECT + subq_8.ds__day + , subq_8.metric_time__day + , subq_8.user + , subq_8.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE metric_time__day = '2020-01-01' + ) subq_8 + ) subq_9 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.metric_time__day + , subq_11.metric_time__week + , subq_11.metric_time__month + , subq_11.metric_time__quarter + , subq_11.metric_time__year + , subq_11.metric_time__extract_year + , subq_11.metric_time__extract_quarter + , subq_11.metric_time__extract_month + , subq_11.metric_time__extract_day + , subq_11.metric_time__extract_dow + , subq_11.metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers + , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.ds__day AS metric_time__day + , subq_10.ds__week AS metric_time__week + , subq_10.ds__month AS metric_time__month + , subq_10.ds__quarter AS metric_time__quarter + , subq_10.ds__year AS metric_time__year + , subq_10.ds__extract_year AS metric_time__extract_year + , subq_10.ds__extract_quarter AS metric_time__extract_quarter + , subq_10.ds__extract_month AS metric_time__extract_month + , subq_10.ds__extract_day AS metric_time__extract_day + , subq_10.ds__extract_dow AS metric_time__extract_dow + , subq_10.ds__extract_doy AS metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_28000.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM buys_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM buys_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM buys_source_src_28000.ds) END AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_28000.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day + , CASE WHEN EXTRACT(dow FROM buys_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM buys_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM buys_source_src_28000.ds) END AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS buy__ds__extract_doy + , buys_source_src_28000.user_id AS user + , buys_source_src_28000.session_id + , buys_source_src_28000.user_id AS buy__user + , buys_source_src_28000.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_10 + ) subq_11 + ) subq_12 + ON + ( + subq_9.user = subq_12.user + ) AND ( + (subq_9.ds__day <= subq_12.ds__day) + ) + ) subq_13 + ) subq_14 + ) subq_15 + ) subq_16 +) subq_17 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_filter__plan0_optimized.sql new file mode 100644 index 0000000000..9bcb14f848 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_filter__plan0_optimized.sql @@ -0,0 +1,103 @@ +-- Combine Aggregated Outputs +-- Compute Metrics via Expressions +SELECT + CAST(MAX(subq_34.buys) AS DOUBLE PRECISION) / CAST(NULLIF(MAX(subq_23.visits), 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate +FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits',] + -- Aggregate Measures + SELECT + 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_20 + WHERE metric_time__day = '2020-01-01' +) subq_23 +CROSS JOIN ( + -- Find conversions for user within the range of INF + -- Pass Only Elements: ['buys',] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_27.visits) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_27.ds__day) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_27.metric_time__day) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS metric_time__day + , FIRST_VALUE(subq_27.user) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_30.mf_internal_uuid AS mf_internal_uuid + , subq_30.buys AS buys + FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] + SELECT + ds__day + , metric_time__day + , subq_25.user + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_25 + WHERE metric_time__day = '2020-01-01' + ) subq_27 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_30 + ON + ( + subq_27.user = subq_30.user + ) AND ( + (subq_27.ds__day <= subq_30.ds__day) + ) + ) subq_31 +) subq_34 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_filter_not_in_group_by__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_filter_not_in_group_by__plan0.sql new file mode 100644 index 0000000000..64a507b609 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_filter_not_in_group_by__plan0.sql @@ -0,0 +1,433 @@ +-- Compute Metrics via Expressions +SELECT + subq_17.buys AS visit_buy_conversions +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_5.visits) AS visits + , COALESCE(MAX(subq_16.buys), 0) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_4.visits) AS visits + FROM ( + -- Pass Only Elements: ['visits',] + SELECT + subq_3.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_2.visit__referrer_id + , subq_2.visits + FROM ( + -- Pass Only Elements: ['visits', 'visit__referrer_id'] + SELECT + subq_1.visit__referrer_id + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_0 + ) subq_1 + ) subq_2 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_3 + ) subq_4 + ) subq_5 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_15.buys) AS buys + FROM ( + -- Pass Only Elements: ['buys',] + SELECT + subq_14.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_13.ds__day + , subq_13.user + , subq_13.visit__referrer_id + , subq_13.buys + , subq_13.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_9.visits) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_9.visit__referrer_id) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visit__referrer_id + , FIRST_VALUE(subq_9.ds__day) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_9.user) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_12.mf_internal_uuid AS mf_internal_uuid + , subq_12.buys AS buys + FROM ( + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] + SELECT + subq_8.ds__day + , subq_8.user + , subq_8.visit__referrer_id + , subq_8.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_8 + ) subq_9 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.metric_time__day + , subq_11.metric_time__week + , subq_11.metric_time__month + , subq_11.metric_time__quarter + , subq_11.metric_time__year + , subq_11.metric_time__extract_year + , subq_11.metric_time__extract_quarter + , subq_11.metric_time__extract_month + , subq_11.metric_time__extract_day + , subq_11.metric_time__extract_dow + , subq_11.metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers + , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.ds__day AS metric_time__day + , subq_10.ds__week AS metric_time__week + , subq_10.ds__month AS metric_time__month + , subq_10.ds__quarter AS metric_time__quarter + , subq_10.ds__year AS metric_time__year + , subq_10.ds__extract_year AS metric_time__extract_year + , subq_10.ds__extract_quarter AS metric_time__extract_quarter + , subq_10.ds__extract_month AS metric_time__extract_month + , subq_10.ds__extract_day AS metric_time__extract_day + , subq_10.ds__extract_dow AS metric_time__extract_dow + , subq_10.ds__extract_doy AS metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_28000.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM buys_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM buys_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM buys_source_src_28000.ds) END AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_28000.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day + , CASE WHEN EXTRACT(dow FROM buys_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM buys_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM buys_source_src_28000.ds) END AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS buy__ds__extract_doy + , buys_source_src_28000.user_id AS user + , buys_source_src_28000.session_id + , buys_source_src_28000.user_id AS buy__user + , buys_source_src_28000.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_10 + ) subq_11 + ) subq_12 + ON + ( + subq_9.user = subq_12.user + ) AND ( + ( + subq_9.ds__day <= subq_12.ds__day + ) AND ( + subq_9.ds__day > DATEADD(day, -7, subq_12.ds__day) + ) + ) + ) subq_13 + ) subq_14 + ) subq_15 + ) subq_16 +) subq_17 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_filter_not_in_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_filter_not_in_group_by__plan0_optimized.sql new file mode 100644 index 0000000000..55aa22e94c --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_filter_not_in_group_by__plan0_optimized.sql @@ -0,0 +1,107 @@ +-- Combine Aggregated Outputs +-- Compute Metrics via Expressions +SELECT + COALESCE(MAX(subq_34.buys), 0) AS visit_buy_conversions +FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits',] + -- Aggregate Measures + SELECT + 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_20 + WHERE visit__referrer_id = 'ref_id_01' +) subq_23 +CROSS JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: ['buys',] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_27.visits) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_27.visit__referrer_id) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visit__referrer_id + , FIRST_VALUE(subq_27.ds__day) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_27.user) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_30.mf_internal_uuid AS mf_internal_uuid + , subq_30.buys AS buys + FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] + SELECT + ds__day + , subq_25.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_25 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_27 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_30 + ON + ( + subq_27.user = subq_30.user + ) AND ( + ( + subq_27.ds__day <= subq_30.ds__day + ) AND ( + subq_27.ds__day > DATEADD(day, -7, subq_30.ds__day) + ) + ) + ) subq_31 +) subq_34 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_time_constraint__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_time_constraint__plan0.sql index 469a9d5c46..97e24c78f0 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_time_constraint__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_time_constraint__plan0.sql @@ -1,13 +1,13 @@ -- Compute Metrics via Expressions SELECT - subq_17.visit__referrer_id - , CAST(subq_17.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_17.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate + subq_18.visit__referrer_id + , CAST(subq_18.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_18.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) AS visit__referrer_id , MAX(subq_5.visits) AS visits - , MAX(subq_16.buys) AS buys + , MAX(subq_17.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -158,277 +158,323 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_15.visit__referrer_id - , SUM(subq_15.buys) AS buys + subq_16.visit__referrer_id + , SUM(subq_16.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'visit__referrer_id'] SELECT - subq_14.visit__referrer_id - , subq_14.buys + subq_15.visit__referrer_id + , subq_15.buys FROM ( -- Find conversions for user within the range of INF SELECT - subq_13.ds__day - , subq_13.user - , subq_13.visit__referrer_id - , subq_13.buys - , subq_13.visits + subq_14.ds__day + , subq_14.user + , subq_14.visit__referrer_id + , subq_14.buys + , subq_14.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_9.visits) OVER ( + FIRST_VALUE(subq_10.visits) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_9.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_10.visit__referrer_id) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_9.ds__day) OVER ( + , FIRST_VALUE(subq_10.ds__day) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_9.user) OVER ( + , FIRST_VALUE(subq_10.user) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_12.mf_internal_uuid AS mf_internal_uuid - , subq_12.buys AS buys + , subq_13.mf_internal_uuid AS mf_internal_uuid + , subq_13.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] SELECT - subq_8.ds__day - , subq_8.user - , subq_8.visit__referrer_id - , subq_8.visits + subq_9.ds__day + , subq_9.user + , subq_9.visit__referrer_id + , subq_9.visits FROM ( - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE SELECT - subq_7.ds__day - , subq_7.ds__week - , subq_7.ds__month - , subq_7.ds__quarter - , subq_7.ds__year - , subq_7.ds__extract_year - , subq_7.ds__extract_quarter - , subq_7.ds__extract_month - , subq_7.ds__extract_day - , subq_7.ds__extract_dow - , subq_7.ds__extract_doy - , subq_7.visit__ds__day - , subq_7.visit__ds__week - , subq_7.visit__ds__month - , subq_7.visit__ds__quarter - , subq_7.visit__ds__year - , subq_7.visit__ds__extract_year - , subq_7.visit__ds__extract_quarter - , subq_7.visit__ds__extract_month - , subq_7.visit__ds__extract_day - , subq_7.visit__ds__extract_dow - , subq_7.visit__ds__extract_doy - , subq_7.metric_time__day - , subq_7.metric_time__week - , subq_7.metric_time__month - , subq_7.metric_time__quarter - , subq_7.metric_time__year - , subq_7.metric_time__extract_year - , subq_7.metric_time__extract_quarter - , subq_7.metric_time__extract_month - , subq_7.metric_time__extract_day - , subq_7.metric_time__extract_dow - , subq_7.metric_time__extract_doy - , subq_7.user - , subq_7.session - , subq_7.visit__user - , subq_7.visit__session - , subq_7.referrer_id - , subq_7.visit__referrer_id - , subq_7.visits - , subq_7.visitors + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.visit__ds__day + , subq_8.visit__ds__week + , subq_8.visit__ds__month + , subq_8.visit__ds__quarter + , subq_8.visit__ds__year + , subq_8.visit__ds__extract_year + , subq_8.visit__ds__extract_quarter + , subq_8.visit__ds__extract_month + , subq_8.visit__ds__extract_day + , subq_8.visit__ds__extract_dow + , subq_8.visit__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session + , subq_8.visit__user + , subq_8.visit__session + , subq_8.referrer_id + , subq_8.visit__referrer_id + , subq_8.visits + , subq_8.visitors FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] SELECT - subq_6.ds__day - , subq_6.ds__week - , subq_6.ds__month - , subq_6.ds__quarter - , subq_6.ds__year - , subq_6.ds__extract_year - , subq_6.ds__extract_quarter - , subq_6.ds__extract_month - , subq_6.ds__extract_day - , subq_6.ds__extract_dow - , subq_6.ds__extract_doy - , subq_6.visit__ds__day - , subq_6.visit__ds__week - , subq_6.visit__ds__month - , subq_6.visit__ds__quarter - , subq_6.visit__ds__year - , subq_6.visit__ds__extract_year - , subq_6.visit__ds__extract_quarter - , subq_6.visit__ds__extract_month - , subq_6.visit__ds__extract_day - , subq_6.visit__ds__extract_dow - , subq_6.visit__ds__extract_doy - , subq_6.ds__day AS metric_time__day - , subq_6.ds__week AS metric_time__week - , subq_6.ds__month AS metric_time__month - , subq_6.ds__quarter AS metric_time__quarter - , subq_6.ds__year AS metric_time__year - , subq_6.ds__extract_year AS metric_time__extract_year - , subq_6.ds__extract_quarter AS metric_time__extract_quarter - , subq_6.ds__extract_month AS metric_time__extract_month - , subq_6.ds__extract_day AS metric_time__extract_day - , subq_6.ds__extract_dow AS metric_time__extract_dow - , subq_6.ds__extract_doy AS metric_time__extract_doy - , subq_6.user - , subq_6.session - , subq_6.visit__user - , subq_6.visit__session - , subq_6.referrer_id - , subq_6.visit__referrer_id - , subq_6.visits - , subq_6.visitors + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_6 - ) subq_7 - WHERE subq_7.metric_time__day BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_8 - ) subq_9 + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE subq_7.metric_time__day BETWEEN '2020-01-01' AND '2020-01-02' + ) subq_8 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_9 + ) subq_10 INNER JOIN ( -- Add column with generated UUID SELECT - subq_11.ds__day - , subq_11.ds__week - , subq_11.ds__month - , subq_11.ds__quarter - , subq_11.ds__year - , subq_11.ds__extract_year - , subq_11.ds__extract_quarter - , subq_11.ds__extract_month - , subq_11.ds__extract_day - , subq_11.ds__extract_dow - , subq_11.ds__extract_doy - , subq_11.buy__ds__day - , subq_11.buy__ds__week - , subq_11.buy__ds__month - , subq_11.buy__ds__quarter - , subq_11.buy__ds__year - , subq_11.buy__ds__extract_year - , subq_11.buy__ds__extract_quarter - , subq_11.buy__ds__extract_month - , subq_11.buy__ds__extract_day - , subq_11.buy__ds__extract_dow - , subq_11.buy__ds__extract_doy - , subq_11.metric_time__day - , subq_11.metric_time__week - , subq_11.metric_time__month - , subq_11.metric_time__quarter - , subq_11.metric_time__year - , subq_11.metric_time__extract_year - , subq_11.metric_time__extract_quarter - , subq_11.metric_time__extract_month - , subq_11.metric_time__extract_day - , subq_11.metric_time__extract_dow - , subq_11.metric_time__extract_doy - , subq_11.user - , subq_11.session_id - , subq_11.buy__user - , subq_11.buy__session_id - , subq_11.buys - , subq_11.buyers + subq_12.ds__day + , subq_12.ds__week + , subq_12.ds__month + , subq_12.ds__quarter + , subq_12.ds__year + , subq_12.ds__extract_year + , subq_12.ds__extract_quarter + , subq_12.ds__extract_month + , subq_12.ds__extract_day + , subq_12.ds__extract_dow + , subq_12.ds__extract_doy + , subq_12.buy__ds__day + , subq_12.buy__ds__week + , subq_12.buy__ds__month + , subq_12.buy__ds__quarter + , subq_12.buy__ds__year + , subq_12.buy__ds__extract_year + , subq_12.buy__ds__extract_quarter + , subq_12.buy__ds__extract_month + , subq_12.buy__ds__extract_day + , subq_12.buy__ds__extract_dow + , subq_12.buy__ds__extract_doy + , subq_12.metric_time__day + , subq_12.metric_time__week + , subq_12.metric_time__month + , subq_12.metric_time__quarter + , subq_12.metric_time__year + , subq_12.metric_time__extract_year + , subq_12.metric_time__extract_quarter + , subq_12.metric_time__extract_month + , subq_12.metric_time__extract_day + , subq_12.metric_time__extract_dow + , subq_12.metric_time__extract_doy + , subq_12.user + , subq_12.session_id + , subq_12.buy__user + , subq_12.buy__session_id + , subq_12.buys + , subq_12.buyers , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_10.ds__day - , subq_10.ds__week - , subq_10.ds__month - , subq_10.ds__quarter - , subq_10.ds__year - , subq_10.ds__extract_year - , subq_10.ds__extract_quarter - , subq_10.ds__extract_month - , subq_10.ds__extract_day - , subq_10.ds__extract_dow - , subq_10.ds__extract_doy - , subq_10.buy__ds__day - , subq_10.buy__ds__week - , subq_10.buy__ds__month - , subq_10.buy__ds__quarter - , subq_10.buy__ds__year - , subq_10.buy__ds__extract_year - , subq_10.buy__ds__extract_quarter - , subq_10.buy__ds__extract_month - , subq_10.buy__ds__extract_day - , subq_10.buy__ds__extract_dow - , subq_10.buy__ds__extract_doy - , subq_10.ds__day AS metric_time__day - , subq_10.ds__week AS metric_time__week - , subq_10.ds__month AS metric_time__month - , subq_10.ds__quarter AS metric_time__quarter - , subq_10.ds__year AS metric_time__year - , subq_10.ds__extract_year AS metric_time__extract_year - , subq_10.ds__extract_quarter AS metric_time__extract_quarter - , subq_10.ds__extract_month AS metric_time__extract_month - , subq_10.ds__extract_day AS metric_time__extract_day - , subq_10.ds__extract_dow AS metric_time__extract_dow - , subq_10.ds__extract_doy AS metric_time__extract_doy - , subq_10.user - , subq_10.session_id - , subq_10.buy__user - , subq_10.buy__session_id - , subq_10.buys - , subq_10.buyers + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.ds__day AS metric_time__day + , subq_11.ds__week AS metric_time__week + , subq_11.ds__month AS metric_time__month + , subq_11.ds__quarter AS metric_time__quarter + , subq_11.ds__year AS metric_time__year + , subq_11.ds__extract_year AS metric_time__extract_year + , subq_11.ds__extract_quarter AS metric_time__extract_quarter + , subq_11.ds__extract_month AS metric_time__extract_month + , subq_11.ds__extract_day AS metric_time__extract_day + , subq_11.ds__extract_dow AS metric_time__extract_dow + , subq_11.ds__extract_doy AS metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -461,23 +507,23 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_10 - ) subq_11 - ) subq_12 + ) subq_11 + ) subq_12 + ) subq_13 ON ( - subq_9.user = subq_12.user + subq_10.user = subq_13.user ) AND ( - (subq_9.ds__day <= subq_12.ds__day) + (subq_10.ds__day <= subq_13.ds__day) ) - ) subq_13 - ) subq_14 - ) subq_15 + ) subq_14 + ) subq_15 + ) subq_16 GROUP BY - subq_15.visit__referrer_id - ) subq_16 + subq_16.visit__referrer_id + ) subq_17 ON - subq_5.visit__referrer_id = subq_16.visit__referrer_id + subq_5.visit__referrer_id = subq_17.visit__referrer_id GROUP BY - COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) -) subq_17 + COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) +) subq_18 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_time_constraint__plan0_optimized.sql index 1cf9773de6..5172ec57f9 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_time_constraint__plan0_optimized.sql @@ -5,9 +5,9 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) AS visit__referrer_id - , MAX(subq_23.visits) AS visits - , MAX(subq_34.buys) AS buys + COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) AS visit__referrer_id + , MAX(subq_24.visits) AS visits + , MAX(subq_36.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -24,11 +24,11 @@ FROM ( , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_21 + ) subq_22 WHERE visit__referrer_id = 'ref_id_01' GROUP BY visit__referrer_id - ) subq_23 + ) subq_24 FULL OUTER JOIN ( -- Find conversions for user within the range of INF -- Pass Only Elements: ['buys', 'visit__referrer_id'] @@ -39,53 +39,62 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_27.visits) OVER ( + FIRST_VALUE(subq_29.visits) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_27.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_29.visit__referrer_id) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_27.ds__day) OVER ( + , FIRST_VALUE(subq_29.ds__day) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_27.user) OVER ( + , FIRST_VALUE(subq_29.user) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_30.mf_internal_uuid AS mf_internal_uuid - , subq_30.buys AS buys + , subq_32.mf_internal_uuid AS mf_internal_uuid + , subq_32.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_27 + ds__day + , subq_27.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' + ) subq_27 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_29 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -96,19 +105,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_30 + ) subq_32 ON ( - subq_27.user = subq_30.user + subq_29.user = subq_32.user ) AND ( - (subq_27.ds__day <= subq_30.ds__day) + (subq_29.ds__day <= subq_32.ds__day) ) - ) subq_31 + ) subq_33 GROUP BY visit__referrer_id - ) subq_34 + ) subq_36 ON - subq_23.visit__referrer_id = subq_34.visit__referrer_id + subq_24.visit__referrer_id = subq_36.visit__referrer_id GROUP BY - COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) -) subq_35 + COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) +) subq_37 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_window__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_window__plan0.sql index f5d8a8c3ff..904c97c297 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_window__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_window__plan0.sql @@ -1,13 +1,13 @@ -- Compute Metrics via Expressions SELECT - subq_15.metric_time__day - , CAST(subq_15.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_15.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_7days + subq_16.metric_time__day + , CAST(subq_16.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_16.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) AS metric_time__day + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) AS metric_time__day , MAX(subq_4.visits) AS visits - , MAX(subq_14.buys) AS buys + , MAX(subq_15.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -112,231 +112,277 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_13.metric_time__day - , SUM(subq_13.buys) AS buys + subq_14.metric_time__day + , SUM(subq_14.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'metric_time__day'] SELECT - subq_12.metric_time__day - , subq_12.buys + subq_13.metric_time__day + , subq_13.buys FROM ( -- Find conversions for user within the range of 7 day SELECT - subq_11.ds__day - , subq_11.metric_time__day - , subq_11.user - , subq_11.buys - , subq_11.visits + subq_12.ds__day + , subq_12.metric_time__day + , subq_12.user + , subq_12.buys + , subq_12.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_7.visits) OVER ( + FIRST_VALUE(subq_8.visits) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_7.ds__day) OVER ( + , FIRST_VALUE(subq_8.ds__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_7.metric_time__day) OVER ( + , FIRST_VALUE(subq_8.metric_time__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_7.user) OVER ( + , FIRST_VALUE(subq_8.user) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_10.mf_internal_uuid AS mf_internal_uuid - , subq_10.buys AS buys + , subq_11.mf_internal_uuid AS mf_internal_uuid + , subq_11.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_6.ds__day - , subq_6.metric_time__day - , subq_6.user - , subq_6.visits + subq_7.ds__day + , subq_7.metric_time__day + , subq_7.user + , subq_7.visits FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE SELECT - subq_5.ds__day - , subq_5.ds__week - , subq_5.ds__month - , subq_5.ds__quarter - , subq_5.ds__year - , subq_5.ds__extract_year - , subq_5.ds__extract_quarter - , subq_5.ds__extract_month - , subq_5.ds__extract_day - , subq_5.ds__extract_dow - , subq_5.ds__extract_doy - , subq_5.visit__ds__day - , subq_5.visit__ds__week - , subq_5.visit__ds__month - , subq_5.visit__ds__quarter - , subq_5.visit__ds__year - , subq_5.visit__ds__extract_year - , subq_5.visit__ds__extract_quarter - , subq_5.visit__ds__extract_month - , subq_5.visit__ds__extract_day - , subq_5.visit__ds__extract_dow - , subq_5.visit__ds__extract_doy - , subq_5.ds__day AS metric_time__day - , subq_5.ds__week AS metric_time__week - , subq_5.ds__month AS metric_time__month - , subq_5.ds__quarter AS metric_time__quarter - , subq_5.ds__year AS metric_time__year - , subq_5.ds__extract_year AS metric_time__extract_year - , subq_5.ds__extract_quarter AS metric_time__extract_quarter - , subq_5.ds__extract_month AS metric_time__extract_month - , subq_5.ds__extract_day AS metric_time__extract_day - , subq_5.ds__extract_dow AS metric_time__extract_dow - , subq_5.ds__extract_doy AS metric_time__extract_doy - , subq_5.user - , subq_5.session - , subq_5.visit__user - , subq_5.visit__session - , subq_5.referrer_id - , subq_5.visit__referrer_id - , subq_5.visits - , subq_5.visitors + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.metric_time__day + , subq_6.metric_time__week + , subq_6.metric_time__month + , subq_6.metric_time__quarter + , subq_6.metric_time__year + , subq_6.metric_time__extract_year + , subq_6.metric_time__extract_quarter + , subq_6.metric_time__extract_month + , subq_6.metric_time__extract_day + , subq_6.metric_time__extract_dow + , subq_6.metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_5 - ) subq_6 - ) subq_7 + subq_5.ds__day + , subq_5.ds__week + , subq_5.ds__month + , subq_5.ds__quarter + , subq_5.ds__year + , subq_5.ds__extract_year + , subq_5.ds__extract_quarter + , subq_5.ds__extract_month + , subq_5.ds__extract_day + , subq_5.ds__extract_dow + , subq_5.ds__extract_doy + , subq_5.visit__ds__day + , subq_5.visit__ds__week + , subq_5.visit__ds__month + , subq_5.visit__ds__quarter + , subq_5.visit__ds__year + , subq_5.visit__ds__extract_year + , subq_5.visit__ds__extract_quarter + , subq_5.visit__ds__extract_month + , subq_5.visit__ds__extract_day + , subq_5.visit__ds__extract_dow + , subq_5.visit__ds__extract_doy + , subq_5.ds__day AS metric_time__day + , subq_5.ds__week AS metric_time__week + , subq_5.ds__month AS metric_time__month + , subq_5.ds__quarter AS metric_time__quarter + , subq_5.ds__year AS metric_time__year + , subq_5.ds__extract_year AS metric_time__extract_year + , subq_5.ds__extract_quarter AS metric_time__extract_quarter + , subq_5.ds__extract_month AS metric_time__extract_month + , subq_5.ds__extract_day AS metric_time__extract_day + , subq_5.ds__extract_dow AS metric_time__extract_dow + , subq_5.ds__extract_doy AS metric_time__extract_doy + , subq_5.user + , subq_5.session + , subq_5.visit__user + , subq_5.visit__session + , subq_5.referrer_id + , subq_5.visit__referrer_id + , subq_5.visits + , subq_5.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_5 + ) subq_6 + WHERE metric_time__day = '2020-01-01' + ) subq_7 + ) subq_8 INNER JOIN ( -- Add column with generated UUID SELECT - subq_9.ds__day - , subq_9.ds__week - , subq_9.ds__month - , subq_9.ds__quarter - , subq_9.ds__year - , subq_9.ds__extract_year - , subq_9.ds__extract_quarter - , subq_9.ds__extract_month - , subq_9.ds__extract_day - , subq_9.ds__extract_dow - , subq_9.ds__extract_doy - , subq_9.buy__ds__day - , subq_9.buy__ds__week - , subq_9.buy__ds__month - , subq_9.buy__ds__quarter - , subq_9.buy__ds__year - , subq_9.buy__ds__extract_year - , subq_9.buy__ds__extract_quarter - , subq_9.buy__ds__extract_month - , subq_9.buy__ds__extract_day - , subq_9.buy__ds__extract_dow - , subq_9.buy__ds__extract_doy - , subq_9.metric_time__day - , subq_9.metric_time__week - , subq_9.metric_time__month - , subq_9.metric_time__quarter - , subq_9.metric_time__year - , subq_9.metric_time__extract_year - , subq_9.metric_time__extract_quarter - , subq_9.metric_time__extract_month - , subq_9.metric_time__extract_day - , subq_9.metric_time__extract_dow - , subq_9.metric_time__extract_doy - , subq_9.user - , subq_9.session_id - , subq_9.buy__user - , subq_9.buy__session_id - , subq_9.buys - , subq_9.buyers + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.metric_time__day + , subq_10.metric_time__week + , subq_10.metric_time__month + , subq_10.metric_time__quarter + , subq_10.metric_time__year + , subq_10.metric_time__extract_year + , subq_10.metric_time__extract_quarter + , subq_10.metric_time__extract_month + , subq_10.metric_time__extract_day + , subq_10.metric_time__extract_dow + , subq_10.metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_8.ds__day - , subq_8.ds__week - , subq_8.ds__month - , subq_8.ds__quarter - , subq_8.ds__year - , subq_8.ds__extract_year - , subq_8.ds__extract_quarter - , subq_8.ds__extract_month - , subq_8.ds__extract_day - , subq_8.ds__extract_dow - , subq_8.ds__extract_doy - , subq_8.buy__ds__day - , subq_8.buy__ds__week - , subq_8.buy__ds__month - , subq_8.buy__ds__quarter - , subq_8.buy__ds__year - , subq_8.buy__ds__extract_year - , subq_8.buy__ds__extract_quarter - , subq_8.buy__ds__extract_month - , subq_8.buy__ds__extract_day - , subq_8.buy__ds__extract_dow - , subq_8.buy__ds__extract_doy - , subq_8.ds__day AS metric_time__day - , subq_8.ds__week AS metric_time__week - , subq_8.ds__month AS metric_time__month - , subq_8.ds__quarter AS metric_time__quarter - , subq_8.ds__year AS metric_time__year - , subq_8.ds__extract_year AS metric_time__extract_year - , subq_8.ds__extract_quarter AS metric_time__extract_quarter - , subq_8.ds__extract_month AS metric_time__extract_month - , subq_8.ds__extract_day AS metric_time__extract_day - , subq_8.ds__extract_dow AS metric_time__extract_dow - , subq_8.ds__extract_doy AS metric_time__extract_doy - , subq_8.user - , subq_8.session_id - , subq_8.buy__user - , subq_8.buy__session_id - , subq_8.buys - , subq_8.buyers + subq_9.ds__day + , subq_9.ds__week + , subq_9.ds__month + , subq_9.ds__quarter + , subq_9.ds__year + , subq_9.ds__extract_year + , subq_9.ds__extract_quarter + , subq_9.ds__extract_month + , subq_9.ds__extract_day + , subq_9.ds__extract_dow + , subq_9.ds__extract_doy + , subq_9.buy__ds__day + , subq_9.buy__ds__week + , subq_9.buy__ds__month + , subq_9.buy__ds__quarter + , subq_9.buy__ds__year + , subq_9.buy__ds__extract_year + , subq_9.buy__ds__extract_quarter + , subq_9.buy__ds__extract_month + , subq_9.buy__ds__extract_day + , subq_9.buy__ds__extract_dow + , subq_9.buy__ds__extract_doy + , subq_9.ds__day AS metric_time__day + , subq_9.ds__week AS metric_time__week + , subq_9.ds__month AS metric_time__month + , subq_9.ds__quarter AS metric_time__quarter + , subq_9.ds__year AS metric_time__year + , subq_9.ds__extract_year AS metric_time__extract_year + , subq_9.ds__extract_quarter AS metric_time__extract_quarter + , subq_9.ds__extract_month AS metric_time__extract_month + , subq_9.ds__extract_day AS metric_time__extract_day + , subq_9.ds__extract_dow AS metric_time__extract_dow + , subq_9.ds__extract_doy AS metric_time__extract_doy + , subq_9.user + , subq_9.session_id + , subq_9.buy__user + , subq_9.buy__session_id + , subq_9.buys + , subq_9.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -369,27 +415,27 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_8 - ) subq_9 - ) subq_10 + ) subq_9 + ) subq_10 + ) subq_11 ON ( - subq_7.user = subq_10.user + subq_8.user = subq_11.user ) AND ( ( - subq_7.ds__day <= subq_10.ds__day + subq_8.ds__day <= subq_11.ds__day ) AND ( - subq_7.ds__day > DATEADD(day, -7, subq_10.ds__day) + subq_8.ds__day > DATEADD(day, -7, subq_11.ds__day) ) ) - ) subq_11 - ) subq_12 - ) subq_13 + ) subq_12 + ) subq_13 + ) subq_14 GROUP BY - subq_13.metric_time__day - ) subq_14 + subq_14.metric_time__day + ) subq_15 ON - subq_4.metric_time__day = subq_14.metric_time__day + subq_4.metric_time__day = subq_15.metric_time__day GROUP BY - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) -) subq_15 + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) +) subq_16 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_window__plan0_optimized.sql index 50f5012370..5177e11ea5 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_window__plan0_optimized.sql @@ -5,9 +5,9 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_20.visits) AS visits - , MAX(subq_30.buys) AS buys + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , MAX(subq_21.visits) AS visits + , MAX(subq_32.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -22,11 +22,11 @@ FROM ( DATE_TRUNC('day', ds) AS metric_time__day , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_18 + ) subq_19 WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__day - ) subq_20 + ) subq_21 FULL OUTER JOIN ( -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'metric_time__day'] @@ -37,51 +37,60 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_23.visits) OVER ( + FIRST_VALUE(subq_25.visits) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_23.ds__day) OVER ( + , FIRST_VALUE(subq_25.ds__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_23.metric_time__day) OVER ( + , FIRST_VALUE(subq_25.metric_time__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_23.user) OVER ( + , FIRST_VALUE(subq_25.user) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_26.mf_internal_uuid AS mf_internal_uuid - , subq_26.buys AS buys + , subq_28.mf_internal_uuid AS mf_internal_uuid + , subq_28.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + ds__day + , metric_time__day + , subq_23.user + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_23 + WHERE metric_time__day = '2020-01-01' + ) subq_25 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -92,23 +101,23 @@ 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_26 + ) subq_28 ON ( - subq_23.user = subq_26.user + subq_25.user = subq_28.user ) AND ( ( - subq_23.ds__day <= subq_26.ds__day + subq_25.ds__day <= subq_28.ds__day ) AND ( - subq_23.ds__day > DATEADD(day, -7, subq_26.ds__day) + subq_25.ds__day > DATEADD(day, -7, subq_28.ds__day) ) ) - ) subq_27 + ) subq_29 GROUP BY metric_time__day - ) subq_30 + ) subq_32 ON - subq_20.metric_time__day = subq_30.metric_time__day + subq_21.metric_time__day = subq_32.metric_time__day GROUP BY - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) -) subq_31 + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) +) subq_33 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_window_and_time_constraint__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_window_and_time_constraint__plan0.sql index 8de5c90870..6b25f06f85 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_window_and_time_constraint__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_window_and_time_constraint__plan0.sql @@ -1,15 +1,15 @@ -- Compute Metrics via Expressions SELECT - subq_17.metric_time__day - , subq_17.visit__referrer_id - , CAST(subq_17.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_17.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_7days + subq_18.metric_time__day + , subq_18.visit__referrer_id + , CAST(subq_18.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_18.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_5.metric_time__day, subq_16.metric_time__day) AS metric_time__day - , COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_5.metric_time__day, subq_17.metric_time__day) AS metric_time__day + , COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) AS visit__referrer_id , MAX(subq_5.visits) AS visits - , MAX(subq_16.buys) AS buys + , MAX(subq_17.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -164,289 +164,335 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_15.metric_time__day - , subq_15.visit__referrer_id - , SUM(subq_15.buys) AS buys + subq_16.metric_time__day + , subq_16.visit__referrer_id + , SUM(subq_16.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] SELECT - subq_14.metric_time__day - , subq_14.visit__referrer_id - , subq_14.buys + subq_15.metric_time__day + , subq_15.visit__referrer_id + , subq_15.buys FROM ( -- Find conversions for user within the range of 7 day SELECT - subq_13.ds__day - , subq_13.metric_time__day - , subq_13.user - , subq_13.visit__referrer_id - , subq_13.buys - , subq_13.visits + subq_14.ds__day + , subq_14.metric_time__day + , subq_14.user + , subq_14.visit__referrer_id + , subq_14.buys + , subq_14.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_9.visits) OVER ( + FIRST_VALUE(subq_10.visits) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_9.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_10.visit__referrer_id) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_9.ds__day) OVER ( + , FIRST_VALUE(subq_10.ds__day) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_9.metric_time__day) OVER ( + , FIRST_VALUE(subq_10.metric_time__day) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_9.user) OVER ( + , FIRST_VALUE(subq_10.user) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_12.mf_internal_uuid AS mf_internal_uuid - , subq_12.buys AS buys + , subq_13.mf_internal_uuid AS mf_internal_uuid + , subq_13.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_8.ds__day - , subq_8.metric_time__day - , subq_8.user - , subq_8.visit__referrer_id - , subq_8.visits + subq_9.ds__day + , subq_9.metric_time__day + , subq_9.user + , subq_9.visit__referrer_id + , subq_9.visits FROM ( - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE SELECT - subq_7.ds__day - , subq_7.ds__week - , subq_7.ds__month - , subq_7.ds__quarter - , subq_7.ds__year - , subq_7.ds__extract_year - , subq_7.ds__extract_quarter - , subq_7.ds__extract_month - , subq_7.ds__extract_day - , subq_7.ds__extract_dow - , subq_7.ds__extract_doy - , subq_7.visit__ds__day - , subq_7.visit__ds__week - , subq_7.visit__ds__month - , subq_7.visit__ds__quarter - , subq_7.visit__ds__year - , subq_7.visit__ds__extract_year - , subq_7.visit__ds__extract_quarter - , subq_7.visit__ds__extract_month - , subq_7.visit__ds__extract_day - , subq_7.visit__ds__extract_dow - , subq_7.visit__ds__extract_doy - , subq_7.metric_time__day - , subq_7.metric_time__week - , subq_7.metric_time__month - , subq_7.metric_time__quarter - , subq_7.metric_time__year - , subq_7.metric_time__extract_year - , subq_7.metric_time__extract_quarter - , subq_7.metric_time__extract_month - , subq_7.metric_time__extract_day - , subq_7.metric_time__extract_dow - , subq_7.metric_time__extract_doy - , subq_7.user - , subq_7.session - , subq_7.visit__user - , subq_7.visit__session - , subq_7.referrer_id - , subq_7.visit__referrer_id - , subq_7.visits - , subq_7.visitors + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.visit__ds__day + , subq_8.visit__ds__week + , subq_8.visit__ds__month + , subq_8.visit__ds__quarter + , subq_8.visit__ds__year + , subq_8.visit__ds__extract_year + , subq_8.visit__ds__extract_quarter + , subq_8.visit__ds__extract_month + , subq_8.visit__ds__extract_day + , subq_8.visit__ds__extract_dow + , subq_8.visit__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session + , subq_8.visit__user + , subq_8.visit__session + , subq_8.referrer_id + , subq_8.visit__referrer_id + , subq_8.visits + , subq_8.visitors FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] SELECT - subq_6.ds__day - , subq_6.ds__week - , subq_6.ds__month - , subq_6.ds__quarter - , subq_6.ds__year - , subq_6.ds__extract_year - , subq_6.ds__extract_quarter - , subq_6.ds__extract_month - , subq_6.ds__extract_day - , subq_6.ds__extract_dow - , subq_6.ds__extract_doy - , subq_6.visit__ds__day - , subq_6.visit__ds__week - , subq_6.visit__ds__month - , subq_6.visit__ds__quarter - , subq_6.visit__ds__year - , subq_6.visit__ds__extract_year - , subq_6.visit__ds__extract_quarter - , subq_6.visit__ds__extract_month - , subq_6.visit__ds__extract_day - , subq_6.visit__ds__extract_dow - , subq_6.visit__ds__extract_doy - , subq_6.ds__day AS metric_time__day - , subq_6.ds__week AS metric_time__week - , subq_6.ds__month AS metric_time__month - , subq_6.ds__quarter AS metric_time__quarter - , subq_6.ds__year AS metric_time__year - , subq_6.ds__extract_year AS metric_time__extract_year - , subq_6.ds__extract_quarter AS metric_time__extract_quarter - , subq_6.ds__extract_month AS metric_time__extract_month - , subq_6.ds__extract_day AS metric_time__extract_day - , subq_6.ds__extract_dow AS metric_time__extract_dow - , subq_6.ds__extract_doy AS metric_time__extract_doy - , subq_6.user - , subq_6.session - , subq_6.visit__user - , subq_6.visit__session - , subq_6.referrer_id - , subq_6.visit__referrer_id - , subq_6.visits - , subq_6.visitors + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_6 - ) subq_7 - WHERE subq_7.metric_time__day BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_8 - ) subq_9 + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE subq_7.metric_time__day BETWEEN '2020-01-01' AND '2020-01-02' + ) subq_8 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_9 + ) subq_10 INNER JOIN ( -- Add column with generated UUID SELECT - subq_11.ds__day - , subq_11.ds__week - , subq_11.ds__month - , subq_11.ds__quarter - , subq_11.ds__year - , subq_11.ds__extract_year - , subq_11.ds__extract_quarter - , subq_11.ds__extract_month - , subq_11.ds__extract_day - , subq_11.ds__extract_dow - , subq_11.ds__extract_doy - , subq_11.buy__ds__day - , subq_11.buy__ds__week - , subq_11.buy__ds__month - , subq_11.buy__ds__quarter - , subq_11.buy__ds__year - , subq_11.buy__ds__extract_year - , subq_11.buy__ds__extract_quarter - , subq_11.buy__ds__extract_month - , subq_11.buy__ds__extract_day - , subq_11.buy__ds__extract_dow - , subq_11.buy__ds__extract_doy - , subq_11.metric_time__day - , subq_11.metric_time__week - , subq_11.metric_time__month - , subq_11.metric_time__quarter - , subq_11.metric_time__year - , subq_11.metric_time__extract_year - , subq_11.metric_time__extract_quarter - , subq_11.metric_time__extract_month - , subq_11.metric_time__extract_day - , subq_11.metric_time__extract_dow - , subq_11.metric_time__extract_doy - , subq_11.user - , subq_11.session_id - , subq_11.buy__user - , subq_11.buy__session_id - , subq_11.buys - , subq_11.buyers + subq_12.ds__day + , subq_12.ds__week + , subq_12.ds__month + , subq_12.ds__quarter + , subq_12.ds__year + , subq_12.ds__extract_year + , subq_12.ds__extract_quarter + , subq_12.ds__extract_month + , subq_12.ds__extract_day + , subq_12.ds__extract_dow + , subq_12.ds__extract_doy + , subq_12.buy__ds__day + , subq_12.buy__ds__week + , subq_12.buy__ds__month + , subq_12.buy__ds__quarter + , subq_12.buy__ds__year + , subq_12.buy__ds__extract_year + , subq_12.buy__ds__extract_quarter + , subq_12.buy__ds__extract_month + , subq_12.buy__ds__extract_day + , subq_12.buy__ds__extract_dow + , subq_12.buy__ds__extract_doy + , subq_12.metric_time__day + , subq_12.metric_time__week + , subq_12.metric_time__month + , subq_12.metric_time__quarter + , subq_12.metric_time__year + , subq_12.metric_time__extract_year + , subq_12.metric_time__extract_quarter + , subq_12.metric_time__extract_month + , subq_12.metric_time__extract_day + , subq_12.metric_time__extract_dow + , subq_12.metric_time__extract_doy + , subq_12.user + , subq_12.session_id + , subq_12.buy__user + , subq_12.buy__session_id + , subq_12.buys + , subq_12.buyers , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_10.ds__day - , subq_10.ds__week - , subq_10.ds__month - , subq_10.ds__quarter - , subq_10.ds__year - , subq_10.ds__extract_year - , subq_10.ds__extract_quarter - , subq_10.ds__extract_month - , subq_10.ds__extract_day - , subq_10.ds__extract_dow - , subq_10.ds__extract_doy - , subq_10.buy__ds__day - , subq_10.buy__ds__week - , subq_10.buy__ds__month - , subq_10.buy__ds__quarter - , subq_10.buy__ds__year - , subq_10.buy__ds__extract_year - , subq_10.buy__ds__extract_quarter - , subq_10.buy__ds__extract_month - , subq_10.buy__ds__extract_day - , subq_10.buy__ds__extract_dow - , subq_10.buy__ds__extract_doy - , subq_10.ds__day AS metric_time__day - , subq_10.ds__week AS metric_time__week - , subq_10.ds__month AS metric_time__month - , subq_10.ds__quarter AS metric_time__quarter - , subq_10.ds__year AS metric_time__year - , subq_10.ds__extract_year AS metric_time__extract_year - , subq_10.ds__extract_quarter AS metric_time__extract_quarter - , subq_10.ds__extract_month AS metric_time__extract_month - , subq_10.ds__extract_day AS metric_time__extract_day - , subq_10.ds__extract_dow AS metric_time__extract_dow - , subq_10.ds__extract_doy AS metric_time__extract_doy - , subq_10.user - , subq_10.session_id - , subq_10.buy__user - , subq_10.buy__session_id - , subq_10.buys - , subq_10.buyers + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.ds__day AS metric_time__day + , subq_11.ds__week AS metric_time__week + , subq_11.ds__month AS metric_time__month + , subq_11.ds__quarter AS metric_time__quarter + , subq_11.ds__year AS metric_time__year + , subq_11.ds__extract_year AS metric_time__extract_year + , subq_11.ds__extract_quarter AS metric_time__extract_quarter + , subq_11.ds__extract_month AS metric_time__extract_month + , subq_11.ds__extract_day AS metric_time__extract_day + , subq_11.ds__extract_dow AS metric_time__extract_dow + , subq_11.ds__extract_doy AS metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -479,33 +525,33 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_10 - ) subq_11 - ) subq_12 + ) subq_11 + ) subq_12 + ) subq_13 ON ( - subq_9.user = subq_12.user + subq_10.user = subq_13.user ) AND ( ( - subq_9.ds__day <= subq_12.ds__day + subq_10.ds__day <= subq_13.ds__day ) AND ( - subq_9.ds__day > DATEADD(day, -7, subq_12.ds__day) + subq_10.ds__day > DATEADD(day, -7, subq_13.ds__day) ) ) - ) subq_13 - ) subq_14 - ) subq_15 + ) subq_14 + ) subq_15 + ) subq_16 GROUP BY - subq_15.metric_time__day - , subq_15.visit__referrer_id - ) subq_16 + subq_16.metric_time__day + , subq_16.visit__referrer_id + ) subq_17 ON ( - subq_5.visit__referrer_id = subq_16.visit__referrer_id + subq_5.visit__referrer_id = subq_17.visit__referrer_id ) AND ( - subq_5.metric_time__day = subq_16.metric_time__day + subq_5.metric_time__day = subq_17.metric_time__day ) GROUP BY - COALESCE(subq_5.metric_time__day, subq_16.metric_time__day) - , COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) -) subq_17 + COALESCE(subq_5.metric_time__day, subq_17.metric_time__day) + , COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) +) subq_18 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql index 424adfe6b6..2929da476f 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql @@ -6,10 +6,10 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_23.metric_time__day, subq_34.metric_time__day) AS metric_time__day - , COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) AS visit__referrer_id - , MAX(subq_23.visits) AS visits - , MAX(subq_34.buys) AS buys + COALESCE(subq_24.metric_time__day, subq_36.metric_time__day) AS metric_time__day + , COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) AS visit__referrer_id + , MAX(subq_24.visits) AS visits + , MAX(subq_36.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -28,12 +28,12 @@ FROM ( , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_21 + ) subq_22 WHERE visit__referrer_id = 'ref_id_01' GROUP BY metric_time__day , visit__referrer_id - ) subq_23 + ) subq_24 FULL OUTER JOIN ( -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] @@ -45,62 +45,72 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_27.visits) OVER ( + FIRST_VALUE(subq_29.visits) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_27.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_29.visit__referrer_id) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_27.ds__day) OVER ( + , FIRST_VALUE(subq_29.ds__day) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_27.metric_time__day) OVER ( + , FIRST_VALUE(subq_29.metric_time__day) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_27.user) OVER ( + , FIRST_VALUE(subq_29.user) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_30.mf_internal_uuid AS mf_internal_uuid - , subq_30.buys AS buys + , subq_32.mf_internal_uuid AS mf_internal_uuid + , subq_32.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_27 + ds__day + , metric_time__day + , subq_27.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' + ) subq_27 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_29 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -111,29 +121,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_30 + ) subq_32 ON ( - subq_27.user = subq_30.user + subq_29.user = subq_32.user ) AND ( ( - subq_27.ds__day <= subq_30.ds__day + subq_29.ds__day <= subq_32.ds__day ) AND ( - subq_27.ds__day > DATEADD(day, -7, subq_30.ds__day) + subq_29.ds__day > DATEADD(day, -7, subq_32.ds__day) ) ) - ) subq_31 + ) subq_33 GROUP BY metric_time__day , visit__referrer_id - ) subq_34 + ) subq_36 ON ( - subq_23.visit__referrer_id = subq_34.visit__referrer_id + subq_24.visit__referrer_id = subq_36.visit__referrer_id ) AND ( - subq_23.metric_time__day = subq_34.metric_time__day + subq_24.metric_time__day = subq_36.metric_time__day ) GROUP BY - COALESCE(subq_23.metric_time__day, subq_34.metric_time__day) - , COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) -) subq_35 + COALESCE(subq_24.metric_time__day, subq_36.metric_time__day) + , COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) +) subq_37 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric__plan0.sql index 5523bdba87..52d82ca11e 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric__plan0.sql @@ -1,13 +1,13 @@ -- Compute Metrics via Expressions SELECT - subq_15.metric_time__day - , CAST(subq_15.buys AS DOUBLE) / CAST(NULLIF(subq_15.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate + subq_16.metric_time__day + , CAST(subq_16.buys AS DOUBLE) / CAST(NULLIF(subq_16.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) AS metric_time__day + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) AS metric_time__day , MAX(subq_4.visits) AS visits - , MAX(subq_14.buys) AS buys + , MAX(subq_15.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -112,231 +112,277 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_13.metric_time__day - , SUM(subq_13.buys) AS buys + subq_14.metric_time__day + , SUM(subq_14.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'metric_time__day'] SELECT - subq_12.metric_time__day - , subq_12.buys + subq_13.metric_time__day + , subq_13.buys FROM ( -- Find conversions for user within the range of INF SELECT - subq_11.ds__day - , subq_11.metric_time__day - , subq_11.user - , subq_11.buys - , subq_11.visits + subq_12.ds__day + , subq_12.metric_time__day + , subq_12.user + , subq_12.buys + , subq_12.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_7.visits) OVER ( + FIRST_VALUE(subq_8.visits) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_7.ds__day) OVER ( + , FIRST_VALUE(subq_8.ds__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_7.metric_time__day) OVER ( + , FIRST_VALUE(subq_8.metric_time__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_7.user) OVER ( + , FIRST_VALUE(subq_8.user) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_10.mf_internal_uuid AS mf_internal_uuid - , subq_10.buys AS buys + , subq_11.mf_internal_uuid AS mf_internal_uuid + , subq_11.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_6.ds__day - , subq_6.metric_time__day - , subq_6.user - , subq_6.visits + subq_7.ds__day + , subq_7.metric_time__day + , subq_7.user + , subq_7.visits FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE SELECT - subq_5.ds__day - , subq_5.ds__week - , subq_5.ds__month - , subq_5.ds__quarter - , subq_5.ds__year - , subq_5.ds__extract_year - , subq_5.ds__extract_quarter - , subq_5.ds__extract_month - , subq_5.ds__extract_day - , subq_5.ds__extract_dow - , subq_5.ds__extract_doy - , subq_5.visit__ds__day - , subq_5.visit__ds__week - , subq_5.visit__ds__month - , subq_5.visit__ds__quarter - , subq_5.visit__ds__year - , subq_5.visit__ds__extract_year - , subq_5.visit__ds__extract_quarter - , subq_5.visit__ds__extract_month - , subq_5.visit__ds__extract_day - , subq_5.visit__ds__extract_dow - , subq_5.visit__ds__extract_doy - , subq_5.ds__day AS metric_time__day - , subq_5.ds__week AS metric_time__week - , subq_5.ds__month AS metric_time__month - , subq_5.ds__quarter AS metric_time__quarter - , subq_5.ds__year AS metric_time__year - , subq_5.ds__extract_year AS metric_time__extract_year - , subq_5.ds__extract_quarter AS metric_time__extract_quarter - , subq_5.ds__extract_month AS metric_time__extract_month - , subq_5.ds__extract_day AS metric_time__extract_day - , subq_5.ds__extract_dow AS metric_time__extract_dow - , subq_5.ds__extract_doy AS metric_time__extract_doy - , subq_5.user - , subq_5.session - , subq_5.visit__user - , subq_5.visit__session - , subq_5.referrer_id - , subq_5.visit__referrer_id - , subq_5.visits - , subq_5.visitors + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.metric_time__day + , subq_6.metric_time__week + , subq_6.metric_time__month + , subq_6.metric_time__quarter + , subq_6.metric_time__year + , subq_6.metric_time__extract_year + , subq_6.metric_time__extract_quarter + , subq_6.metric_time__extract_month + , subq_6.metric_time__extract_day + , subq_6.metric_time__extract_dow + , subq_6.metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_5 - ) subq_6 - ) subq_7 + subq_5.ds__day + , subq_5.ds__week + , subq_5.ds__month + , subq_5.ds__quarter + , subq_5.ds__year + , subq_5.ds__extract_year + , subq_5.ds__extract_quarter + , subq_5.ds__extract_month + , subq_5.ds__extract_day + , subq_5.ds__extract_dow + , subq_5.ds__extract_doy + , subq_5.visit__ds__day + , subq_5.visit__ds__week + , subq_5.visit__ds__month + , subq_5.visit__ds__quarter + , subq_5.visit__ds__year + , subq_5.visit__ds__extract_year + , subq_5.visit__ds__extract_quarter + , subq_5.visit__ds__extract_month + , subq_5.visit__ds__extract_day + , subq_5.visit__ds__extract_dow + , subq_5.visit__ds__extract_doy + , subq_5.ds__day AS metric_time__day + , subq_5.ds__week AS metric_time__week + , subq_5.ds__month AS metric_time__month + , subq_5.ds__quarter AS metric_time__quarter + , subq_5.ds__year AS metric_time__year + , subq_5.ds__extract_year AS metric_time__extract_year + , subq_5.ds__extract_quarter AS metric_time__extract_quarter + , subq_5.ds__extract_month AS metric_time__extract_month + , subq_5.ds__extract_day AS metric_time__extract_day + , subq_5.ds__extract_dow AS metric_time__extract_dow + , subq_5.ds__extract_doy AS metric_time__extract_doy + , subq_5.user + , subq_5.session + , subq_5.visit__user + , subq_5.visit__session + , subq_5.referrer_id + , subq_5.visit__referrer_id + , subq_5.visits + , subq_5.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_5 + ) subq_6 + WHERE metric_time__day = '2020-01-01' + ) subq_7 + ) subq_8 INNER JOIN ( -- Add column with generated UUID SELECT - subq_9.ds__day - , subq_9.ds__week - , subq_9.ds__month - , subq_9.ds__quarter - , subq_9.ds__year - , subq_9.ds__extract_year - , subq_9.ds__extract_quarter - , subq_9.ds__extract_month - , subq_9.ds__extract_day - , subq_9.ds__extract_dow - , subq_9.ds__extract_doy - , subq_9.buy__ds__day - , subq_9.buy__ds__week - , subq_9.buy__ds__month - , subq_9.buy__ds__quarter - , subq_9.buy__ds__year - , subq_9.buy__ds__extract_year - , subq_9.buy__ds__extract_quarter - , subq_9.buy__ds__extract_month - , subq_9.buy__ds__extract_day - , subq_9.buy__ds__extract_dow - , subq_9.buy__ds__extract_doy - , subq_9.metric_time__day - , subq_9.metric_time__week - , subq_9.metric_time__month - , subq_9.metric_time__quarter - , subq_9.metric_time__year - , subq_9.metric_time__extract_year - , subq_9.metric_time__extract_quarter - , subq_9.metric_time__extract_month - , subq_9.metric_time__extract_day - , subq_9.metric_time__extract_dow - , subq_9.metric_time__extract_doy - , subq_9.user - , subq_9.session_id - , subq_9.buy__user - , subq_9.buy__session_id - , subq_9.buys - , subq_9.buyers + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.metric_time__day + , subq_10.metric_time__week + , subq_10.metric_time__month + , subq_10.metric_time__quarter + , subq_10.metric_time__year + , subq_10.metric_time__extract_year + , subq_10.metric_time__extract_quarter + , subq_10.metric_time__extract_month + , subq_10.metric_time__extract_day + , subq_10.metric_time__extract_dow + , subq_10.metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers , UUID_STRING() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_8.ds__day - , subq_8.ds__week - , subq_8.ds__month - , subq_8.ds__quarter - , subq_8.ds__year - , subq_8.ds__extract_year - , subq_8.ds__extract_quarter - , subq_8.ds__extract_month - , subq_8.ds__extract_day - , subq_8.ds__extract_dow - , subq_8.ds__extract_doy - , subq_8.buy__ds__day - , subq_8.buy__ds__week - , subq_8.buy__ds__month - , subq_8.buy__ds__quarter - , subq_8.buy__ds__year - , subq_8.buy__ds__extract_year - , subq_8.buy__ds__extract_quarter - , subq_8.buy__ds__extract_month - , subq_8.buy__ds__extract_day - , subq_8.buy__ds__extract_dow - , subq_8.buy__ds__extract_doy - , subq_8.ds__day AS metric_time__day - , subq_8.ds__week AS metric_time__week - , subq_8.ds__month AS metric_time__month - , subq_8.ds__quarter AS metric_time__quarter - , subq_8.ds__year AS metric_time__year - , subq_8.ds__extract_year AS metric_time__extract_year - , subq_8.ds__extract_quarter AS metric_time__extract_quarter - , subq_8.ds__extract_month AS metric_time__extract_month - , subq_8.ds__extract_day AS metric_time__extract_day - , subq_8.ds__extract_dow AS metric_time__extract_dow - , subq_8.ds__extract_doy AS metric_time__extract_doy - , subq_8.user - , subq_8.session_id - , subq_8.buy__user - , subq_8.buy__session_id - , subq_8.buys - , subq_8.buyers + subq_9.ds__day + , subq_9.ds__week + , subq_9.ds__month + , subq_9.ds__quarter + , subq_9.ds__year + , subq_9.ds__extract_year + , subq_9.ds__extract_quarter + , subq_9.ds__extract_month + , subq_9.ds__extract_day + , subq_9.ds__extract_dow + , subq_9.ds__extract_doy + , subq_9.buy__ds__day + , subq_9.buy__ds__week + , subq_9.buy__ds__month + , subq_9.buy__ds__quarter + , subq_9.buy__ds__year + , subq_9.buy__ds__extract_year + , subq_9.buy__ds__extract_quarter + , subq_9.buy__ds__extract_month + , subq_9.buy__ds__extract_day + , subq_9.buy__ds__extract_dow + , subq_9.buy__ds__extract_doy + , subq_9.ds__day AS metric_time__day + , subq_9.ds__week AS metric_time__week + , subq_9.ds__month AS metric_time__month + , subq_9.ds__quarter AS metric_time__quarter + , subq_9.ds__year AS metric_time__year + , subq_9.ds__extract_year AS metric_time__extract_year + , subq_9.ds__extract_quarter AS metric_time__extract_quarter + , subq_9.ds__extract_month AS metric_time__extract_month + , subq_9.ds__extract_day AS metric_time__extract_day + , subq_9.ds__extract_dow AS metric_time__extract_dow + , subq_9.ds__extract_doy AS metric_time__extract_doy + , subq_9.user + , subq_9.session_id + , subq_9.buy__user + , subq_9.buy__session_id + , subq_9.buys + , subq_9.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -369,23 +415,23 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_8 - ) subq_9 - ) subq_10 + ) subq_9 + ) subq_10 + ) subq_11 ON ( - subq_7.user = subq_10.user + subq_8.user = subq_11.user ) AND ( - (subq_7.ds__day <= subq_10.ds__day) + (subq_8.ds__day <= subq_11.ds__day) ) - ) subq_11 - ) subq_12 - ) subq_13 + ) subq_12 + ) subq_13 + ) subq_14 GROUP BY - subq_13.metric_time__day - ) subq_14 + subq_14.metric_time__day + ) subq_15 ON - subq_4.metric_time__day = subq_14.metric_time__day + subq_4.metric_time__day = subq_15.metric_time__day GROUP BY - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) -) subq_15 + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) +) subq_16 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric__plan0_optimized.sql index 7e54f9421a..3cf5465df2 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric__plan0_optimized.sql @@ -5,9 +5,9 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_20.visits) AS visits - , MAX(subq_30.buys) AS buys + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , MAX(subq_21.visits) AS visits + , MAX(subq_32.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -22,11 +22,11 @@ FROM ( DATE_TRUNC('day', ds) AS metric_time__day , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_18 + ) subq_19 WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__day - ) subq_20 + ) subq_21 FULL OUTER JOIN ( -- Find conversions for user within the range of INF -- Pass Only Elements: ['buys', 'metric_time__day'] @@ -37,51 +37,60 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_23.visits) OVER ( + FIRST_VALUE(subq_25.visits) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_23.ds__day) OVER ( + , FIRST_VALUE(subq_25.ds__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_23.metric_time__day) OVER ( + , FIRST_VALUE(subq_25.metric_time__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_23.user) OVER ( + , FIRST_VALUE(subq_25.user) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_26.mf_internal_uuid AS mf_internal_uuid - , subq_26.buys AS buys + , subq_28.mf_internal_uuid AS mf_internal_uuid + , subq_28.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + ds__day + , metric_time__day + , subq_23.user + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_23 + WHERE metric_time__day = '2020-01-01' + ) subq_25 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -92,19 +101,19 @@ FROM ( , 1 AS buys , UUID_STRING() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_26 + ) subq_28 ON ( - subq_23.user = subq_26.user + subq_25.user = subq_28.user ) AND ( - (subq_23.ds__day <= subq_26.ds__day) + (subq_25.ds__day <= subq_28.ds__day) ) - ) subq_27 + ) subq_29 GROUP BY metric_time__day - ) subq_30 + ) subq_32 ON - subq_20.metric_time__day = subq_30.metric_time__day + subq_21.metric_time__day = subq_32.metric_time__day GROUP BY - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) -) subq_31 + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) +) subq_33 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_categorical_filter__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_categorical_filter__plan0.sql index c834aff166..fce09bfdc3 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_categorical_filter__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_categorical_filter__plan0.sql @@ -1,15 +1,15 @@ -- Compute Metrics via Expressions SELECT - subq_15.metric_time__day - , subq_15.visit__referrer_id - , CAST(subq_15.buys AS DOUBLE) / CAST(NULLIF(subq_15.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate + subq_16.metric_time__day + , subq_16.visit__referrer_id + , CAST(subq_16.buys AS DOUBLE) / CAST(NULLIF(subq_16.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) AS metric_time__day - , COALESCE(subq_4.visit__referrer_id, subq_14.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) AS metric_time__day + , COALESCE(subq_4.visit__referrer_id, subq_15.visit__referrer_id) AS visit__referrer_id , MAX(subq_4.visits) AS visits - , MAX(subq_14.buys) AS buys + , MAX(subq_15.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -118,243 +118,289 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_13.metric_time__day - , subq_13.visit__referrer_id - , SUM(subq_13.buys) AS buys + subq_14.metric_time__day + , subq_14.visit__referrer_id + , SUM(subq_14.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] SELECT - subq_12.metric_time__day - , subq_12.visit__referrer_id - , subq_12.buys + subq_13.metric_time__day + , subq_13.visit__referrer_id + , subq_13.buys FROM ( -- Find conversions for user within the range of INF SELECT - subq_11.ds__day - , subq_11.metric_time__day - , subq_11.user - , subq_11.visit__referrer_id - , subq_11.buys - , subq_11.visits + subq_12.ds__day + , subq_12.metric_time__day + , subq_12.user + , subq_12.visit__referrer_id + , subq_12.buys + , subq_12.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_7.visits) OVER ( + FIRST_VALUE(subq_8.visits) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_7.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_8.visit__referrer_id) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_7.ds__day) OVER ( + , FIRST_VALUE(subq_8.ds__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_7.metric_time__day) OVER ( + , FIRST_VALUE(subq_8.metric_time__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_7.user) OVER ( + , FIRST_VALUE(subq_8.user) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_10.mf_internal_uuid AS mf_internal_uuid - , subq_10.buys AS buys + , subq_11.mf_internal_uuid AS mf_internal_uuid + , subq_11.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_6.ds__day - , subq_6.metric_time__day - , subq_6.user - , subq_6.visit__referrer_id - , subq_6.visits + subq_7.ds__day + , subq_7.metric_time__day + , subq_7.user + , subq_7.visit__referrer_id + , subq_7.visits FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE SELECT - subq_5.ds__day - , subq_5.ds__week - , subq_5.ds__month - , subq_5.ds__quarter - , subq_5.ds__year - , subq_5.ds__extract_year - , subq_5.ds__extract_quarter - , subq_5.ds__extract_month - , subq_5.ds__extract_day - , subq_5.ds__extract_dow - , subq_5.ds__extract_doy - , subq_5.visit__ds__day - , subq_5.visit__ds__week - , subq_5.visit__ds__month - , subq_5.visit__ds__quarter - , subq_5.visit__ds__year - , subq_5.visit__ds__extract_year - , subq_5.visit__ds__extract_quarter - , subq_5.visit__ds__extract_month - , subq_5.visit__ds__extract_day - , subq_5.visit__ds__extract_dow - , subq_5.visit__ds__extract_doy - , subq_5.ds__day AS metric_time__day - , subq_5.ds__week AS metric_time__week - , subq_5.ds__month AS metric_time__month - , subq_5.ds__quarter AS metric_time__quarter - , subq_5.ds__year AS metric_time__year - , subq_5.ds__extract_year AS metric_time__extract_year - , subq_5.ds__extract_quarter AS metric_time__extract_quarter - , subq_5.ds__extract_month AS metric_time__extract_month - , subq_5.ds__extract_day AS metric_time__extract_day - , subq_5.ds__extract_dow AS metric_time__extract_dow - , subq_5.ds__extract_doy AS metric_time__extract_doy - , subq_5.user - , subq_5.session - , subq_5.visit__user - , subq_5.visit__session - , subq_5.referrer_id - , subq_5.visit__referrer_id - , subq_5.visits - , subq_5.visitors + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.metric_time__day + , subq_6.metric_time__week + , subq_6.metric_time__month + , subq_6.metric_time__quarter + , subq_6.metric_time__year + , subq_6.metric_time__extract_year + , subq_6.metric_time__extract_quarter + , subq_6.metric_time__extract_month + , subq_6.metric_time__extract_day + , subq_6.metric_time__extract_dow + , subq_6.metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_5 - ) subq_6 - ) subq_7 + subq_5.ds__day + , subq_5.ds__week + , subq_5.ds__month + , subq_5.ds__quarter + , subq_5.ds__year + , subq_5.ds__extract_year + , subq_5.ds__extract_quarter + , subq_5.ds__extract_month + , subq_5.ds__extract_day + , subq_5.ds__extract_dow + , subq_5.ds__extract_doy + , subq_5.visit__ds__day + , subq_5.visit__ds__week + , subq_5.visit__ds__month + , subq_5.visit__ds__quarter + , subq_5.visit__ds__year + , subq_5.visit__ds__extract_year + , subq_5.visit__ds__extract_quarter + , subq_5.visit__ds__extract_month + , subq_5.visit__ds__extract_day + , subq_5.visit__ds__extract_dow + , subq_5.visit__ds__extract_doy + , subq_5.ds__day AS metric_time__day + , subq_5.ds__week AS metric_time__week + , subq_5.ds__month AS metric_time__month + , subq_5.ds__quarter AS metric_time__quarter + , subq_5.ds__year AS metric_time__year + , subq_5.ds__extract_year AS metric_time__extract_year + , subq_5.ds__extract_quarter AS metric_time__extract_quarter + , subq_5.ds__extract_month AS metric_time__extract_month + , subq_5.ds__extract_day AS metric_time__extract_day + , subq_5.ds__extract_dow AS metric_time__extract_dow + , subq_5.ds__extract_doy AS metric_time__extract_doy + , subq_5.user + , subq_5.session + , subq_5.visit__user + , subq_5.visit__session + , subq_5.referrer_id + , subq_5.visit__referrer_id + , subq_5.visits + , subq_5.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_5 + ) subq_6 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_7 + ) subq_8 INNER JOIN ( -- Add column with generated UUID SELECT - subq_9.ds__day - , subq_9.ds__week - , subq_9.ds__month - , subq_9.ds__quarter - , subq_9.ds__year - , subq_9.ds__extract_year - , subq_9.ds__extract_quarter - , subq_9.ds__extract_month - , subq_9.ds__extract_day - , subq_9.ds__extract_dow - , subq_9.ds__extract_doy - , subq_9.buy__ds__day - , subq_9.buy__ds__week - , subq_9.buy__ds__month - , subq_9.buy__ds__quarter - , subq_9.buy__ds__year - , subq_9.buy__ds__extract_year - , subq_9.buy__ds__extract_quarter - , subq_9.buy__ds__extract_month - , subq_9.buy__ds__extract_day - , subq_9.buy__ds__extract_dow - , subq_9.buy__ds__extract_doy - , subq_9.metric_time__day - , subq_9.metric_time__week - , subq_9.metric_time__month - , subq_9.metric_time__quarter - , subq_9.metric_time__year - , subq_9.metric_time__extract_year - , subq_9.metric_time__extract_quarter - , subq_9.metric_time__extract_month - , subq_9.metric_time__extract_day - , subq_9.metric_time__extract_dow - , subq_9.metric_time__extract_doy - , subq_9.user - , subq_9.session_id - , subq_9.buy__user - , subq_9.buy__session_id - , subq_9.buys - , subq_9.buyers + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.metric_time__day + , subq_10.metric_time__week + , subq_10.metric_time__month + , subq_10.metric_time__quarter + , subq_10.metric_time__year + , subq_10.metric_time__extract_year + , subq_10.metric_time__extract_quarter + , subq_10.metric_time__extract_month + , subq_10.metric_time__extract_day + , subq_10.metric_time__extract_dow + , subq_10.metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers , UUID_STRING() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_8.ds__day - , subq_8.ds__week - , subq_8.ds__month - , subq_8.ds__quarter - , subq_8.ds__year - , subq_8.ds__extract_year - , subq_8.ds__extract_quarter - , subq_8.ds__extract_month - , subq_8.ds__extract_day - , subq_8.ds__extract_dow - , subq_8.ds__extract_doy - , subq_8.buy__ds__day - , subq_8.buy__ds__week - , subq_8.buy__ds__month - , subq_8.buy__ds__quarter - , subq_8.buy__ds__year - , subq_8.buy__ds__extract_year - , subq_8.buy__ds__extract_quarter - , subq_8.buy__ds__extract_month - , subq_8.buy__ds__extract_day - , subq_8.buy__ds__extract_dow - , subq_8.buy__ds__extract_doy - , subq_8.ds__day AS metric_time__day - , subq_8.ds__week AS metric_time__week - , subq_8.ds__month AS metric_time__month - , subq_8.ds__quarter AS metric_time__quarter - , subq_8.ds__year AS metric_time__year - , subq_8.ds__extract_year AS metric_time__extract_year - , subq_8.ds__extract_quarter AS metric_time__extract_quarter - , subq_8.ds__extract_month AS metric_time__extract_month - , subq_8.ds__extract_day AS metric_time__extract_day - , subq_8.ds__extract_dow AS metric_time__extract_dow - , subq_8.ds__extract_doy AS metric_time__extract_doy - , subq_8.user - , subq_8.session_id - , subq_8.buy__user - , subq_8.buy__session_id - , subq_8.buys - , subq_8.buyers + subq_9.ds__day + , subq_9.ds__week + , subq_9.ds__month + , subq_9.ds__quarter + , subq_9.ds__year + , subq_9.ds__extract_year + , subq_9.ds__extract_quarter + , subq_9.ds__extract_month + , subq_9.ds__extract_day + , subq_9.ds__extract_dow + , subq_9.ds__extract_doy + , subq_9.buy__ds__day + , subq_9.buy__ds__week + , subq_9.buy__ds__month + , subq_9.buy__ds__quarter + , subq_9.buy__ds__year + , subq_9.buy__ds__extract_year + , subq_9.buy__ds__extract_quarter + , subq_9.buy__ds__extract_month + , subq_9.buy__ds__extract_day + , subq_9.buy__ds__extract_dow + , subq_9.buy__ds__extract_doy + , subq_9.ds__day AS metric_time__day + , subq_9.ds__week AS metric_time__week + , subq_9.ds__month AS metric_time__month + , subq_9.ds__quarter AS metric_time__quarter + , subq_9.ds__year AS metric_time__year + , subq_9.ds__extract_year AS metric_time__extract_year + , subq_9.ds__extract_quarter AS metric_time__extract_quarter + , subq_9.ds__extract_month AS metric_time__extract_month + , subq_9.ds__extract_day AS metric_time__extract_day + , subq_9.ds__extract_dow AS metric_time__extract_dow + , subq_9.ds__extract_doy AS metric_time__extract_doy + , subq_9.user + , subq_9.session_id + , subq_9.buy__user + , subq_9.buy__session_id + , subq_9.buys + , subq_9.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -387,29 +433,29 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_8 - ) subq_9 - ) subq_10 + ) subq_9 + ) subq_10 + ) subq_11 ON ( - subq_7.user = subq_10.user + subq_8.user = subq_11.user ) AND ( - (subq_7.ds__day <= subq_10.ds__day) + (subq_8.ds__day <= subq_11.ds__day) ) - ) subq_11 - ) subq_12 - ) subq_13 + ) subq_12 + ) subq_13 + ) subq_14 GROUP BY - subq_13.metric_time__day - , subq_13.visit__referrer_id - ) subq_14 + subq_14.metric_time__day + , subq_14.visit__referrer_id + ) subq_15 ON ( - subq_4.visit__referrer_id = subq_14.visit__referrer_id + subq_4.visit__referrer_id = subq_15.visit__referrer_id ) AND ( - subq_4.metric_time__day = subq_14.metric_time__day + subq_4.metric_time__day = subq_15.metric_time__day ) GROUP BY - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) - , COALESCE(subq_4.visit__referrer_id, subq_14.visit__referrer_id) -) subq_15 + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) + , COALESCE(subq_4.visit__referrer_id, subq_15.visit__referrer_id) +) subq_16 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_categorical_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_categorical_filter__plan0_optimized.sql index 9abb791996..32b8b8b67b 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_categorical_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_categorical_filter__plan0_optimized.sql @@ -6,10 +6,10 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , COALESCE(subq_20.visit__referrer_id, subq_30.visit__referrer_id) AS visit__referrer_id - , MAX(subq_20.visits) AS visits - , MAX(subq_30.buys) AS buys + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , COALESCE(subq_21.visit__referrer_id, subq_32.visit__referrer_id) AS visit__referrer_id + , MAX(subq_21.visits) AS visits + , MAX(subq_32.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -26,12 +26,12 @@ FROM ( , referrer_id AS visit__referrer_id , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_18 + ) subq_19 WHERE visit__referrer_id = 'ref_id_01' GROUP BY metric_time__day , visit__referrer_id - ) subq_20 + ) subq_21 FULL OUTER JOIN ( -- Find conversions for user within the range of INF -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] @@ -43,60 +43,70 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_23.visits) OVER ( + FIRST_VALUE(subq_25.visits) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_23.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_25.visit__referrer_id) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_23.ds__day) OVER ( + , FIRST_VALUE(subq_25.ds__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_23.metric_time__day) OVER ( + , FIRST_VALUE(subq_25.metric_time__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_23.user) OVER ( + , FIRST_VALUE(subq_25.user) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_26.mf_internal_uuid AS mf_internal_uuid - , subq_26.buys AS buys + , subq_28.mf_internal_uuid AS mf_internal_uuid + , subq_28.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + ds__day + , metric_time__day + , subq_23.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_23 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_25 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -107,25 +117,25 @@ FROM ( , 1 AS buys , UUID_STRING() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_26 + ) subq_28 ON ( - subq_23.user = subq_26.user + subq_25.user = subq_28.user ) AND ( - (subq_23.ds__day <= subq_26.ds__day) + (subq_25.ds__day <= subq_28.ds__day) ) - ) subq_27 + ) subq_29 GROUP BY metric_time__day , visit__referrer_id - ) subq_30 + ) subq_32 ON ( - subq_20.visit__referrer_id = subq_30.visit__referrer_id + subq_21.visit__referrer_id = subq_32.visit__referrer_id ) AND ( - subq_20.metric_time__day = subq_30.metric_time__day + subq_21.metric_time__day = subq_32.metric_time__day ) GROUP BY - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) - , COALESCE(subq_20.visit__referrer_id, subq_30.visit__referrer_id) -) subq_31 + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) + , COALESCE(subq_21.visit__referrer_id, subq_32.visit__referrer_id) +) subq_33 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_filter__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_filter__plan0.sql new file mode 100644 index 0000000000..5d753f31dc --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_filter__plan0.sql @@ -0,0 +1,429 @@ +-- Compute Metrics via Expressions +SELECT + CAST(subq_17.buys AS DOUBLE) / CAST(NULLIF(subq_17.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_5.visits) AS visits + , MAX(subq_16.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_4.visits) AS visits + FROM ( + -- Pass Only Elements: ['visits',] + SELECT + subq_3.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_2.metric_time__day + , subq_2.visits + FROM ( + -- Pass Only Elements: ['visits', 'metric_time__day'] + SELECT + subq_1.metric_time__day + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_0 + ) subq_1 + ) subq_2 + WHERE metric_time__day = '2020-01-01' + ) subq_3 + ) subq_4 + ) subq_5 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_15.buys) AS buys + FROM ( + -- Pass Only Elements: ['buys',] + SELECT + subq_14.buys + FROM ( + -- Find conversions for user within the range of INF + SELECT + subq_13.ds__day + , subq_13.metric_time__day + , subq_13.user + , subq_13.buys + , subq_13.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_9.visits) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_9.ds__day) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_9.metric_time__day) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS metric_time__day + , FIRST_VALUE(subq_9.user) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_12.mf_internal_uuid AS mf_internal_uuid + , subq_12.buys AS buys + FROM ( + -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] + SELECT + subq_8.ds__day + , subq_8.metric_time__day + , subq_8.user + , subq_8.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE metric_time__day = '2020-01-01' + ) subq_8 + ) subq_9 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.metric_time__day + , subq_11.metric_time__week + , subq_11.metric_time__month + , subq_11.metric_time__quarter + , subq_11.metric_time__year + , subq_11.metric_time__extract_year + , subq_11.metric_time__extract_quarter + , subq_11.metric_time__extract_month + , subq_11.metric_time__extract_day + , subq_11.metric_time__extract_dow + , subq_11.metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers + , UUID_STRING() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.ds__day AS metric_time__day + , subq_10.ds__week AS metric_time__week + , subq_10.ds__month AS metric_time__month + , subq_10.ds__quarter AS metric_time__quarter + , subq_10.ds__year AS metric_time__year + , subq_10.ds__extract_year AS metric_time__extract_year + , subq_10.ds__extract_quarter AS metric_time__extract_quarter + , subq_10.ds__extract_month AS metric_time__extract_month + , subq_10.ds__extract_day AS metric_time__extract_day + , subq_10.ds__extract_dow AS metric_time__extract_dow + , subq_10.ds__extract_doy AS metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_28000.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM buys_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_28000.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day + , EXTRACT(dayofweekiso FROM buys_source_src_28000.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS buy__ds__extract_doy + , buys_source_src_28000.user_id AS user + , buys_source_src_28000.session_id + , buys_source_src_28000.user_id AS buy__user + , buys_source_src_28000.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_10 + ) subq_11 + ) subq_12 + ON + ( + subq_9.user = subq_12.user + ) AND ( + (subq_9.ds__day <= subq_12.ds__day) + ) + ) subq_13 + ) subq_14 + ) subq_15 + ) subq_16 +) subq_17 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_filter__plan0_optimized.sql new file mode 100644 index 0000000000..135bdd3747 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_filter__plan0_optimized.sql @@ -0,0 +1,103 @@ +-- Combine Aggregated Outputs +-- Compute Metrics via Expressions +SELECT + CAST(MAX(subq_34.buys) AS DOUBLE) / CAST(NULLIF(MAX(subq_23.visits), 0) AS DOUBLE) AS visit_buy_conversion_rate +FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits',] + -- Aggregate Measures + SELECT + 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_20 + WHERE metric_time__day = '2020-01-01' +) subq_23 +CROSS JOIN ( + -- Find conversions for user within the range of INF + -- Pass Only Elements: ['buys',] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_27.visits) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_27.ds__day) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_27.metric_time__day) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS metric_time__day + , FIRST_VALUE(subq_27.user) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_30.mf_internal_uuid AS mf_internal_uuid + , subq_30.buys AS buys + FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] + SELECT + ds__day + , metric_time__day + , subq_25.user + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_25 + WHERE metric_time__day = '2020-01-01' + ) subq_27 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , UUID_STRING() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_30 + ON + ( + subq_27.user = subq_30.user + ) AND ( + (subq_27.ds__day <= subq_30.ds__day) + ) + ) subq_31 +) subq_34 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_filter_not_in_group_by__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_filter_not_in_group_by__plan0.sql new file mode 100644 index 0000000000..dfe591e240 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_filter_not_in_group_by__plan0.sql @@ -0,0 +1,433 @@ +-- Compute Metrics via Expressions +SELECT + subq_17.buys AS visit_buy_conversions +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_5.visits) AS visits + , COALESCE(MAX(subq_16.buys), 0) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_4.visits) AS visits + FROM ( + -- Pass Only Elements: ['visits',] + SELECT + subq_3.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_2.visit__referrer_id + , subq_2.visits + FROM ( + -- Pass Only Elements: ['visits', 'visit__referrer_id'] + SELECT + subq_1.visit__referrer_id + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_0 + ) subq_1 + ) subq_2 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_3 + ) subq_4 + ) subq_5 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_15.buys) AS buys + FROM ( + -- Pass Only Elements: ['buys',] + SELECT + subq_14.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_13.ds__day + , subq_13.user + , subq_13.visit__referrer_id + , subq_13.buys + , subq_13.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_9.visits) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_9.visit__referrer_id) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visit__referrer_id + , FIRST_VALUE(subq_9.ds__day) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_9.user) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_12.mf_internal_uuid AS mf_internal_uuid + , subq_12.buys AS buys + FROM ( + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] + SELECT + subq_8.ds__day + , subq_8.user + , subq_8.visit__referrer_id + , subq_8.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_8 + ) subq_9 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.metric_time__day + , subq_11.metric_time__week + , subq_11.metric_time__month + , subq_11.metric_time__quarter + , subq_11.metric_time__year + , subq_11.metric_time__extract_year + , subq_11.metric_time__extract_quarter + , subq_11.metric_time__extract_month + , subq_11.metric_time__extract_day + , subq_11.metric_time__extract_dow + , subq_11.metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers + , UUID_STRING() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.ds__day AS metric_time__day + , subq_10.ds__week AS metric_time__week + , subq_10.ds__month AS metric_time__month + , subq_10.ds__quarter AS metric_time__quarter + , subq_10.ds__year AS metric_time__year + , subq_10.ds__extract_year AS metric_time__extract_year + , subq_10.ds__extract_quarter AS metric_time__extract_quarter + , subq_10.ds__extract_month AS metric_time__extract_month + , subq_10.ds__extract_day AS metric_time__extract_day + , subq_10.ds__extract_dow AS metric_time__extract_dow + , subq_10.ds__extract_doy AS metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_28000.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM buys_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_28000.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day + , EXTRACT(dayofweekiso FROM buys_source_src_28000.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS buy__ds__extract_doy + , buys_source_src_28000.user_id AS user + , buys_source_src_28000.session_id + , buys_source_src_28000.user_id AS buy__user + , buys_source_src_28000.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_10 + ) subq_11 + ) subq_12 + ON + ( + subq_9.user = subq_12.user + ) AND ( + ( + subq_9.ds__day <= subq_12.ds__day + ) AND ( + subq_9.ds__day > DATEADD(day, -7, subq_12.ds__day) + ) + ) + ) subq_13 + ) subq_14 + ) subq_15 + ) subq_16 +) subq_17 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_filter_not_in_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_filter_not_in_group_by__plan0_optimized.sql new file mode 100644 index 0000000000..520f4116a7 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_filter_not_in_group_by__plan0_optimized.sql @@ -0,0 +1,107 @@ +-- Combine Aggregated Outputs +-- Compute Metrics via Expressions +SELECT + COALESCE(MAX(subq_34.buys), 0) AS visit_buy_conversions +FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits',] + -- Aggregate Measures + SELECT + 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_20 + WHERE visit__referrer_id = 'ref_id_01' +) subq_23 +CROSS JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: ['buys',] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_27.visits) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_27.visit__referrer_id) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visit__referrer_id + , FIRST_VALUE(subq_27.ds__day) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_27.user) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_30.mf_internal_uuid AS mf_internal_uuid + , subq_30.buys AS buys + FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] + SELECT + ds__day + , subq_25.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_25 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_27 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , UUID_STRING() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_30 + ON + ( + subq_27.user = subq_30.user + ) AND ( + ( + subq_27.ds__day <= subq_30.ds__day + ) AND ( + subq_27.ds__day > DATEADD(day, -7, subq_30.ds__day) + ) + ) + ) subq_31 +) subq_34 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_time_constraint__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_time_constraint__plan0.sql index f7d795ed46..cce4aadfe9 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_time_constraint__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_time_constraint__plan0.sql @@ -1,13 +1,13 @@ -- Compute Metrics via Expressions SELECT - subq_17.visit__referrer_id - , CAST(subq_17.buys AS DOUBLE) / CAST(NULLIF(subq_17.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate + subq_18.visit__referrer_id + , CAST(subq_18.buys AS DOUBLE) / CAST(NULLIF(subq_18.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) AS visit__referrer_id , MAX(subq_5.visits) AS visits - , MAX(subq_16.buys) AS buys + , MAX(subq_17.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -158,277 +158,323 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_15.visit__referrer_id - , SUM(subq_15.buys) AS buys + subq_16.visit__referrer_id + , SUM(subq_16.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'visit__referrer_id'] SELECT - subq_14.visit__referrer_id - , subq_14.buys + subq_15.visit__referrer_id + , subq_15.buys FROM ( -- Find conversions for user within the range of INF SELECT - subq_13.ds__day - , subq_13.user - , subq_13.visit__referrer_id - , subq_13.buys - , subq_13.visits + subq_14.ds__day + , subq_14.user + , subq_14.visit__referrer_id + , subq_14.buys + , subq_14.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_9.visits) OVER ( + FIRST_VALUE(subq_10.visits) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_9.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_10.visit__referrer_id) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_9.ds__day) OVER ( + , FIRST_VALUE(subq_10.ds__day) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_9.user) OVER ( + , FIRST_VALUE(subq_10.user) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_12.mf_internal_uuid AS mf_internal_uuid - , subq_12.buys AS buys + , subq_13.mf_internal_uuid AS mf_internal_uuid + , subq_13.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] SELECT - subq_8.ds__day - , subq_8.user - , subq_8.visit__referrer_id - , subq_8.visits + subq_9.ds__day + , subq_9.user + , subq_9.visit__referrer_id + , subq_9.visits FROM ( - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE SELECT - subq_7.ds__day - , subq_7.ds__week - , subq_7.ds__month - , subq_7.ds__quarter - , subq_7.ds__year - , subq_7.ds__extract_year - , subq_7.ds__extract_quarter - , subq_7.ds__extract_month - , subq_7.ds__extract_day - , subq_7.ds__extract_dow - , subq_7.ds__extract_doy - , subq_7.visit__ds__day - , subq_7.visit__ds__week - , subq_7.visit__ds__month - , subq_7.visit__ds__quarter - , subq_7.visit__ds__year - , subq_7.visit__ds__extract_year - , subq_7.visit__ds__extract_quarter - , subq_7.visit__ds__extract_month - , subq_7.visit__ds__extract_day - , subq_7.visit__ds__extract_dow - , subq_7.visit__ds__extract_doy - , subq_7.metric_time__day - , subq_7.metric_time__week - , subq_7.metric_time__month - , subq_7.metric_time__quarter - , subq_7.metric_time__year - , subq_7.metric_time__extract_year - , subq_7.metric_time__extract_quarter - , subq_7.metric_time__extract_month - , subq_7.metric_time__extract_day - , subq_7.metric_time__extract_dow - , subq_7.metric_time__extract_doy - , subq_7.user - , subq_7.session - , subq_7.visit__user - , subq_7.visit__session - , subq_7.referrer_id - , subq_7.visit__referrer_id - , subq_7.visits - , subq_7.visitors + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.visit__ds__day + , subq_8.visit__ds__week + , subq_8.visit__ds__month + , subq_8.visit__ds__quarter + , subq_8.visit__ds__year + , subq_8.visit__ds__extract_year + , subq_8.visit__ds__extract_quarter + , subq_8.visit__ds__extract_month + , subq_8.visit__ds__extract_day + , subq_8.visit__ds__extract_dow + , subq_8.visit__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session + , subq_8.visit__user + , subq_8.visit__session + , subq_8.referrer_id + , subq_8.visit__referrer_id + , subq_8.visits + , subq_8.visitors FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] SELECT - subq_6.ds__day - , subq_6.ds__week - , subq_6.ds__month - , subq_6.ds__quarter - , subq_6.ds__year - , subq_6.ds__extract_year - , subq_6.ds__extract_quarter - , subq_6.ds__extract_month - , subq_6.ds__extract_day - , subq_6.ds__extract_dow - , subq_6.ds__extract_doy - , subq_6.visit__ds__day - , subq_6.visit__ds__week - , subq_6.visit__ds__month - , subq_6.visit__ds__quarter - , subq_6.visit__ds__year - , subq_6.visit__ds__extract_year - , subq_6.visit__ds__extract_quarter - , subq_6.visit__ds__extract_month - , subq_6.visit__ds__extract_day - , subq_6.visit__ds__extract_dow - , subq_6.visit__ds__extract_doy - , subq_6.ds__day AS metric_time__day - , subq_6.ds__week AS metric_time__week - , subq_6.ds__month AS metric_time__month - , subq_6.ds__quarter AS metric_time__quarter - , subq_6.ds__year AS metric_time__year - , subq_6.ds__extract_year AS metric_time__extract_year - , subq_6.ds__extract_quarter AS metric_time__extract_quarter - , subq_6.ds__extract_month AS metric_time__extract_month - , subq_6.ds__extract_day AS metric_time__extract_day - , subq_6.ds__extract_dow AS metric_time__extract_dow - , subq_6.ds__extract_doy AS metric_time__extract_doy - , subq_6.user - , subq_6.session - , subq_6.visit__user - , subq_6.visit__session - , subq_6.referrer_id - , subq_6.visit__referrer_id - , subq_6.visits - , subq_6.visitors + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_6 - ) subq_7 - WHERE subq_7.metric_time__day BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_8 - ) subq_9 + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE subq_7.metric_time__day BETWEEN '2020-01-01' AND '2020-01-02' + ) subq_8 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_9 + ) subq_10 INNER JOIN ( -- Add column with generated UUID SELECT - subq_11.ds__day - , subq_11.ds__week - , subq_11.ds__month - , subq_11.ds__quarter - , subq_11.ds__year - , subq_11.ds__extract_year - , subq_11.ds__extract_quarter - , subq_11.ds__extract_month - , subq_11.ds__extract_day - , subq_11.ds__extract_dow - , subq_11.ds__extract_doy - , subq_11.buy__ds__day - , subq_11.buy__ds__week - , subq_11.buy__ds__month - , subq_11.buy__ds__quarter - , subq_11.buy__ds__year - , subq_11.buy__ds__extract_year - , subq_11.buy__ds__extract_quarter - , subq_11.buy__ds__extract_month - , subq_11.buy__ds__extract_day - , subq_11.buy__ds__extract_dow - , subq_11.buy__ds__extract_doy - , subq_11.metric_time__day - , subq_11.metric_time__week - , subq_11.metric_time__month - , subq_11.metric_time__quarter - , subq_11.metric_time__year - , subq_11.metric_time__extract_year - , subq_11.metric_time__extract_quarter - , subq_11.metric_time__extract_month - , subq_11.metric_time__extract_day - , subq_11.metric_time__extract_dow - , subq_11.metric_time__extract_doy - , subq_11.user - , subq_11.session_id - , subq_11.buy__user - , subq_11.buy__session_id - , subq_11.buys - , subq_11.buyers + subq_12.ds__day + , subq_12.ds__week + , subq_12.ds__month + , subq_12.ds__quarter + , subq_12.ds__year + , subq_12.ds__extract_year + , subq_12.ds__extract_quarter + , subq_12.ds__extract_month + , subq_12.ds__extract_day + , subq_12.ds__extract_dow + , subq_12.ds__extract_doy + , subq_12.buy__ds__day + , subq_12.buy__ds__week + , subq_12.buy__ds__month + , subq_12.buy__ds__quarter + , subq_12.buy__ds__year + , subq_12.buy__ds__extract_year + , subq_12.buy__ds__extract_quarter + , subq_12.buy__ds__extract_month + , subq_12.buy__ds__extract_day + , subq_12.buy__ds__extract_dow + , subq_12.buy__ds__extract_doy + , subq_12.metric_time__day + , subq_12.metric_time__week + , subq_12.metric_time__month + , subq_12.metric_time__quarter + , subq_12.metric_time__year + , subq_12.metric_time__extract_year + , subq_12.metric_time__extract_quarter + , subq_12.metric_time__extract_month + , subq_12.metric_time__extract_day + , subq_12.metric_time__extract_dow + , subq_12.metric_time__extract_doy + , subq_12.user + , subq_12.session_id + , subq_12.buy__user + , subq_12.buy__session_id + , subq_12.buys + , subq_12.buyers , UUID_STRING() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_10.ds__day - , subq_10.ds__week - , subq_10.ds__month - , subq_10.ds__quarter - , subq_10.ds__year - , subq_10.ds__extract_year - , subq_10.ds__extract_quarter - , subq_10.ds__extract_month - , subq_10.ds__extract_day - , subq_10.ds__extract_dow - , subq_10.ds__extract_doy - , subq_10.buy__ds__day - , subq_10.buy__ds__week - , subq_10.buy__ds__month - , subq_10.buy__ds__quarter - , subq_10.buy__ds__year - , subq_10.buy__ds__extract_year - , subq_10.buy__ds__extract_quarter - , subq_10.buy__ds__extract_month - , subq_10.buy__ds__extract_day - , subq_10.buy__ds__extract_dow - , subq_10.buy__ds__extract_doy - , subq_10.ds__day AS metric_time__day - , subq_10.ds__week AS metric_time__week - , subq_10.ds__month AS metric_time__month - , subq_10.ds__quarter AS metric_time__quarter - , subq_10.ds__year AS metric_time__year - , subq_10.ds__extract_year AS metric_time__extract_year - , subq_10.ds__extract_quarter AS metric_time__extract_quarter - , subq_10.ds__extract_month AS metric_time__extract_month - , subq_10.ds__extract_day AS metric_time__extract_day - , subq_10.ds__extract_dow AS metric_time__extract_dow - , subq_10.ds__extract_doy AS metric_time__extract_doy - , subq_10.user - , subq_10.session_id - , subq_10.buy__user - , subq_10.buy__session_id - , subq_10.buys - , subq_10.buyers + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.ds__day AS metric_time__day + , subq_11.ds__week AS metric_time__week + , subq_11.ds__month AS metric_time__month + , subq_11.ds__quarter AS metric_time__quarter + , subq_11.ds__year AS metric_time__year + , subq_11.ds__extract_year AS metric_time__extract_year + , subq_11.ds__extract_quarter AS metric_time__extract_quarter + , subq_11.ds__extract_month AS metric_time__extract_month + , subq_11.ds__extract_day AS metric_time__extract_day + , subq_11.ds__extract_dow AS metric_time__extract_dow + , subq_11.ds__extract_doy AS metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -461,23 +507,23 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_10 - ) subq_11 - ) subq_12 + ) subq_11 + ) subq_12 + ) subq_13 ON ( - subq_9.user = subq_12.user + subq_10.user = subq_13.user ) AND ( - (subq_9.ds__day <= subq_12.ds__day) + (subq_10.ds__day <= subq_13.ds__day) ) - ) subq_13 - ) subq_14 - ) subq_15 + ) subq_14 + ) subq_15 + ) subq_16 GROUP BY - subq_15.visit__referrer_id - ) subq_16 + subq_16.visit__referrer_id + ) subq_17 ON - subq_5.visit__referrer_id = subq_16.visit__referrer_id + subq_5.visit__referrer_id = subq_17.visit__referrer_id GROUP BY - COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) -) subq_17 + COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) +) subq_18 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_time_constraint__plan0_optimized.sql index 915716e167..6794fc19ba 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_time_constraint__plan0_optimized.sql @@ -5,9 +5,9 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) AS visit__referrer_id - , MAX(subq_23.visits) AS visits - , MAX(subq_34.buys) AS buys + COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) AS visit__referrer_id + , MAX(subq_24.visits) AS visits + , MAX(subq_36.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -24,11 +24,11 @@ FROM ( , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_21 + ) subq_22 WHERE visit__referrer_id = 'ref_id_01' GROUP BY visit__referrer_id - ) subq_23 + ) subq_24 FULL OUTER JOIN ( -- Find conversions for user within the range of INF -- Pass Only Elements: ['buys', 'visit__referrer_id'] @@ -39,53 +39,62 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_27.visits) OVER ( + FIRST_VALUE(subq_29.visits) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_27.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_29.visit__referrer_id) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_27.ds__day) OVER ( + , FIRST_VALUE(subq_29.ds__day) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_27.user) OVER ( + , FIRST_VALUE(subq_29.user) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_30.mf_internal_uuid AS mf_internal_uuid - , subq_30.buys AS buys + , subq_32.mf_internal_uuid AS mf_internal_uuid + , subq_32.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_27 + ds__day + , subq_27.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' + ) subq_27 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_29 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -96,19 +105,19 @@ FROM ( , 1 AS buys , UUID_STRING() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_30 + ) subq_32 ON ( - subq_27.user = subq_30.user + subq_29.user = subq_32.user ) AND ( - (subq_27.ds__day <= subq_30.ds__day) + (subq_29.ds__day <= subq_32.ds__day) ) - ) subq_31 + ) subq_33 GROUP BY visit__referrer_id - ) subq_34 + ) subq_36 ON - subq_23.visit__referrer_id = subq_34.visit__referrer_id + subq_24.visit__referrer_id = subq_36.visit__referrer_id GROUP BY - COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) -) subq_35 + COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) +) subq_37 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_window__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_window__plan0.sql index 3187ba717e..03cdbeb870 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_window__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_window__plan0.sql @@ -1,13 +1,13 @@ -- Compute Metrics via Expressions SELECT - subq_15.metric_time__day - , CAST(subq_15.buys AS DOUBLE) / CAST(NULLIF(subq_15.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days + subq_16.metric_time__day + , CAST(subq_16.buys AS DOUBLE) / CAST(NULLIF(subq_16.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) AS metric_time__day + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) AS metric_time__day , MAX(subq_4.visits) AS visits - , MAX(subq_14.buys) AS buys + , MAX(subq_15.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -112,231 +112,277 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_13.metric_time__day - , SUM(subq_13.buys) AS buys + subq_14.metric_time__day + , SUM(subq_14.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'metric_time__day'] SELECT - subq_12.metric_time__day - , subq_12.buys + subq_13.metric_time__day + , subq_13.buys FROM ( -- Find conversions for user within the range of 7 day SELECT - subq_11.ds__day - , subq_11.metric_time__day - , subq_11.user - , subq_11.buys - , subq_11.visits + subq_12.ds__day + , subq_12.metric_time__day + , subq_12.user + , subq_12.buys + , subq_12.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_7.visits) OVER ( + FIRST_VALUE(subq_8.visits) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_7.ds__day) OVER ( + , FIRST_VALUE(subq_8.ds__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_7.metric_time__day) OVER ( + , FIRST_VALUE(subq_8.metric_time__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_7.user) OVER ( + , FIRST_VALUE(subq_8.user) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_10.mf_internal_uuid AS mf_internal_uuid - , subq_10.buys AS buys + , subq_11.mf_internal_uuid AS mf_internal_uuid + , subq_11.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_6.ds__day - , subq_6.metric_time__day - , subq_6.user - , subq_6.visits + subq_7.ds__day + , subq_7.metric_time__day + , subq_7.user + , subq_7.visits FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE SELECT - subq_5.ds__day - , subq_5.ds__week - , subq_5.ds__month - , subq_5.ds__quarter - , subq_5.ds__year - , subq_5.ds__extract_year - , subq_5.ds__extract_quarter - , subq_5.ds__extract_month - , subq_5.ds__extract_day - , subq_5.ds__extract_dow - , subq_5.ds__extract_doy - , subq_5.visit__ds__day - , subq_5.visit__ds__week - , subq_5.visit__ds__month - , subq_5.visit__ds__quarter - , subq_5.visit__ds__year - , subq_5.visit__ds__extract_year - , subq_5.visit__ds__extract_quarter - , subq_5.visit__ds__extract_month - , subq_5.visit__ds__extract_day - , subq_5.visit__ds__extract_dow - , subq_5.visit__ds__extract_doy - , subq_5.ds__day AS metric_time__day - , subq_5.ds__week AS metric_time__week - , subq_5.ds__month AS metric_time__month - , subq_5.ds__quarter AS metric_time__quarter - , subq_5.ds__year AS metric_time__year - , subq_5.ds__extract_year AS metric_time__extract_year - , subq_5.ds__extract_quarter AS metric_time__extract_quarter - , subq_5.ds__extract_month AS metric_time__extract_month - , subq_5.ds__extract_day AS metric_time__extract_day - , subq_5.ds__extract_dow AS metric_time__extract_dow - , subq_5.ds__extract_doy AS metric_time__extract_doy - , subq_5.user - , subq_5.session - , subq_5.visit__user - , subq_5.visit__session - , subq_5.referrer_id - , subq_5.visit__referrer_id - , subq_5.visits - , subq_5.visitors + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.metric_time__day + , subq_6.metric_time__week + , subq_6.metric_time__month + , subq_6.metric_time__quarter + , subq_6.metric_time__year + , subq_6.metric_time__extract_year + , subq_6.metric_time__extract_quarter + , subq_6.metric_time__extract_month + , subq_6.metric_time__extract_day + , subq_6.metric_time__extract_dow + , subq_6.metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_5 - ) subq_6 - ) subq_7 + subq_5.ds__day + , subq_5.ds__week + , subq_5.ds__month + , subq_5.ds__quarter + , subq_5.ds__year + , subq_5.ds__extract_year + , subq_5.ds__extract_quarter + , subq_5.ds__extract_month + , subq_5.ds__extract_day + , subq_5.ds__extract_dow + , subq_5.ds__extract_doy + , subq_5.visit__ds__day + , subq_5.visit__ds__week + , subq_5.visit__ds__month + , subq_5.visit__ds__quarter + , subq_5.visit__ds__year + , subq_5.visit__ds__extract_year + , subq_5.visit__ds__extract_quarter + , subq_5.visit__ds__extract_month + , subq_5.visit__ds__extract_day + , subq_5.visit__ds__extract_dow + , subq_5.visit__ds__extract_doy + , subq_5.ds__day AS metric_time__day + , subq_5.ds__week AS metric_time__week + , subq_5.ds__month AS metric_time__month + , subq_5.ds__quarter AS metric_time__quarter + , subq_5.ds__year AS metric_time__year + , subq_5.ds__extract_year AS metric_time__extract_year + , subq_5.ds__extract_quarter AS metric_time__extract_quarter + , subq_5.ds__extract_month AS metric_time__extract_month + , subq_5.ds__extract_day AS metric_time__extract_day + , subq_5.ds__extract_dow AS metric_time__extract_dow + , subq_5.ds__extract_doy AS metric_time__extract_doy + , subq_5.user + , subq_5.session + , subq_5.visit__user + , subq_5.visit__session + , subq_5.referrer_id + , subq_5.visit__referrer_id + , subq_5.visits + , subq_5.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_5 + ) subq_6 + WHERE metric_time__day = '2020-01-01' + ) subq_7 + ) subq_8 INNER JOIN ( -- Add column with generated UUID SELECT - subq_9.ds__day - , subq_9.ds__week - , subq_9.ds__month - , subq_9.ds__quarter - , subq_9.ds__year - , subq_9.ds__extract_year - , subq_9.ds__extract_quarter - , subq_9.ds__extract_month - , subq_9.ds__extract_day - , subq_9.ds__extract_dow - , subq_9.ds__extract_doy - , subq_9.buy__ds__day - , subq_9.buy__ds__week - , subq_9.buy__ds__month - , subq_9.buy__ds__quarter - , subq_9.buy__ds__year - , subq_9.buy__ds__extract_year - , subq_9.buy__ds__extract_quarter - , subq_9.buy__ds__extract_month - , subq_9.buy__ds__extract_day - , subq_9.buy__ds__extract_dow - , subq_9.buy__ds__extract_doy - , subq_9.metric_time__day - , subq_9.metric_time__week - , subq_9.metric_time__month - , subq_9.metric_time__quarter - , subq_9.metric_time__year - , subq_9.metric_time__extract_year - , subq_9.metric_time__extract_quarter - , subq_9.metric_time__extract_month - , subq_9.metric_time__extract_day - , subq_9.metric_time__extract_dow - , subq_9.metric_time__extract_doy - , subq_9.user - , subq_9.session_id - , subq_9.buy__user - , subq_9.buy__session_id - , subq_9.buys - , subq_9.buyers + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.metric_time__day + , subq_10.metric_time__week + , subq_10.metric_time__month + , subq_10.metric_time__quarter + , subq_10.metric_time__year + , subq_10.metric_time__extract_year + , subq_10.metric_time__extract_quarter + , subq_10.metric_time__extract_month + , subq_10.metric_time__extract_day + , subq_10.metric_time__extract_dow + , subq_10.metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers , UUID_STRING() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_8.ds__day - , subq_8.ds__week - , subq_8.ds__month - , subq_8.ds__quarter - , subq_8.ds__year - , subq_8.ds__extract_year - , subq_8.ds__extract_quarter - , subq_8.ds__extract_month - , subq_8.ds__extract_day - , subq_8.ds__extract_dow - , subq_8.ds__extract_doy - , subq_8.buy__ds__day - , subq_8.buy__ds__week - , subq_8.buy__ds__month - , subq_8.buy__ds__quarter - , subq_8.buy__ds__year - , subq_8.buy__ds__extract_year - , subq_8.buy__ds__extract_quarter - , subq_8.buy__ds__extract_month - , subq_8.buy__ds__extract_day - , subq_8.buy__ds__extract_dow - , subq_8.buy__ds__extract_doy - , subq_8.ds__day AS metric_time__day - , subq_8.ds__week AS metric_time__week - , subq_8.ds__month AS metric_time__month - , subq_8.ds__quarter AS metric_time__quarter - , subq_8.ds__year AS metric_time__year - , subq_8.ds__extract_year AS metric_time__extract_year - , subq_8.ds__extract_quarter AS metric_time__extract_quarter - , subq_8.ds__extract_month AS metric_time__extract_month - , subq_8.ds__extract_day AS metric_time__extract_day - , subq_8.ds__extract_dow AS metric_time__extract_dow - , subq_8.ds__extract_doy AS metric_time__extract_doy - , subq_8.user - , subq_8.session_id - , subq_8.buy__user - , subq_8.buy__session_id - , subq_8.buys - , subq_8.buyers + subq_9.ds__day + , subq_9.ds__week + , subq_9.ds__month + , subq_9.ds__quarter + , subq_9.ds__year + , subq_9.ds__extract_year + , subq_9.ds__extract_quarter + , subq_9.ds__extract_month + , subq_9.ds__extract_day + , subq_9.ds__extract_dow + , subq_9.ds__extract_doy + , subq_9.buy__ds__day + , subq_9.buy__ds__week + , subq_9.buy__ds__month + , subq_9.buy__ds__quarter + , subq_9.buy__ds__year + , subq_9.buy__ds__extract_year + , subq_9.buy__ds__extract_quarter + , subq_9.buy__ds__extract_month + , subq_9.buy__ds__extract_day + , subq_9.buy__ds__extract_dow + , subq_9.buy__ds__extract_doy + , subq_9.ds__day AS metric_time__day + , subq_9.ds__week AS metric_time__week + , subq_9.ds__month AS metric_time__month + , subq_9.ds__quarter AS metric_time__quarter + , subq_9.ds__year AS metric_time__year + , subq_9.ds__extract_year AS metric_time__extract_year + , subq_9.ds__extract_quarter AS metric_time__extract_quarter + , subq_9.ds__extract_month AS metric_time__extract_month + , subq_9.ds__extract_day AS metric_time__extract_day + , subq_9.ds__extract_dow AS metric_time__extract_dow + , subq_9.ds__extract_doy AS metric_time__extract_doy + , subq_9.user + , subq_9.session_id + , subq_9.buy__user + , subq_9.buy__session_id + , subq_9.buys + , subq_9.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -369,27 +415,27 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_8 - ) subq_9 - ) subq_10 + ) subq_9 + ) subq_10 + ) subq_11 ON ( - subq_7.user = subq_10.user + subq_8.user = subq_11.user ) AND ( ( - subq_7.ds__day <= subq_10.ds__day + subq_8.ds__day <= subq_11.ds__day ) AND ( - subq_7.ds__day > DATEADD(day, -7, subq_10.ds__day) + subq_8.ds__day > DATEADD(day, -7, subq_11.ds__day) ) ) - ) subq_11 - ) subq_12 - ) subq_13 + ) subq_12 + ) subq_13 + ) subq_14 GROUP BY - subq_13.metric_time__day - ) subq_14 + subq_14.metric_time__day + ) subq_15 ON - subq_4.metric_time__day = subq_14.metric_time__day + subq_4.metric_time__day = subq_15.metric_time__day GROUP BY - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) -) subq_15 + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) +) subq_16 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_window__plan0_optimized.sql index 63b4b1290d..f73433d33c 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_window__plan0_optimized.sql @@ -5,9 +5,9 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_20.visits) AS visits - , MAX(subq_30.buys) AS buys + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , MAX(subq_21.visits) AS visits + , MAX(subq_32.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -22,11 +22,11 @@ FROM ( DATE_TRUNC('day', ds) AS metric_time__day , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_18 + ) subq_19 WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__day - ) subq_20 + ) subq_21 FULL OUTER JOIN ( -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'metric_time__day'] @@ -37,51 +37,60 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_23.visits) OVER ( + FIRST_VALUE(subq_25.visits) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_23.ds__day) OVER ( + , FIRST_VALUE(subq_25.ds__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_23.metric_time__day) OVER ( + , FIRST_VALUE(subq_25.metric_time__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_23.user) OVER ( + , FIRST_VALUE(subq_25.user) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_26.mf_internal_uuid AS mf_internal_uuid - , subq_26.buys AS buys + , subq_28.mf_internal_uuid AS mf_internal_uuid + , subq_28.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + ds__day + , metric_time__day + , subq_23.user + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_23 + WHERE metric_time__day = '2020-01-01' + ) subq_25 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -92,23 +101,23 @@ FROM ( , 1 AS buys , UUID_STRING() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_26 + ) subq_28 ON ( - subq_23.user = subq_26.user + subq_25.user = subq_28.user ) AND ( ( - subq_23.ds__day <= subq_26.ds__day + subq_25.ds__day <= subq_28.ds__day ) AND ( - subq_23.ds__day > DATEADD(day, -7, subq_26.ds__day) + subq_25.ds__day > DATEADD(day, -7, subq_28.ds__day) ) ) - ) subq_27 + ) subq_29 GROUP BY metric_time__day - ) subq_30 + ) subq_32 ON - subq_20.metric_time__day = subq_30.metric_time__day + subq_21.metric_time__day = subq_32.metric_time__day GROUP BY - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) -) subq_31 + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) +) subq_33 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_window_and_time_constraint__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_window_and_time_constraint__plan0.sql index b6c8d12e0a..3bd7ccf595 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_window_and_time_constraint__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_window_and_time_constraint__plan0.sql @@ -1,15 +1,15 @@ -- Compute Metrics via Expressions SELECT - subq_17.metric_time__day - , subq_17.visit__referrer_id - , CAST(subq_17.buys AS DOUBLE) / CAST(NULLIF(subq_17.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days + subq_18.metric_time__day + , subq_18.visit__referrer_id + , CAST(subq_18.buys AS DOUBLE) / CAST(NULLIF(subq_18.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_5.metric_time__day, subq_16.metric_time__day) AS metric_time__day - , COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_5.metric_time__day, subq_17.metric_time__day) AS metric_time__day + , COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) AS visit__referrer_id , MAX(subq_5.visits) AS visits - , MAX(subq_16.buys) AS buys + , MAX(subq_17.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -164,289 +164,335 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_15.metric_time__day - , subq_15.visit__referrer_id - , SUM(subq_15.buys) AS buys + subq_16.metric_time__day + , subq_16.visit__referrer_id + , SUM(subq_16.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] SELECT - subq_14.metric_time__day - , subq_14.visit__referrer_id - , subq_14.buys + subq_15.metric_time__day + , subq_15.visit__referrer_id + , subq_15.buys FROM ( -- Find conversions for user within the range of 7 day SELECT - subq_13.ds__day - , subq_13.metric_time__day - , subq_13.user - , subq_13.visit__referrer_id - , subq_13.buys - , subq_13.visits + subq_14.ds__day + , subq_14.metric_time__day + , subq_14.user + , subq_14.visit__referrer_id + , subq_14.buys + , subq_14.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_9.visits) OVER ( + FIRST_VALUE(subq_10.visits) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_9.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_10.visit__referrer_id) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_9.ds__day) OVER ( + , FIRST_VALUE(subq_10.ds__day) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_9.metric_time__day) OVER ( + , FIRST_VALUE(subq_10.metric_time__day) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_9.user) OVER ( + , FIRST_VALUE(subq_10.user) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_12.mf_internal_uuid AS mf_internal_uuid - , subq_12.buys AS buys + , subq_13.mf_internal_uuid AS mf_internal_uuid + , subq_13.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_8.ds__day - , subq_8.metric_time__day - , subq_8.user - , subq_8.visit__referrer_id - , subq_8.visits + subq_9.ds__day + , subq_9.metric_time__day + , subq_9.user + , subq_9.visit__referrer_id + , subq_9.visits FROM ( - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE SELECT - subq_7.ds__day - , subq_7.ds__week - , subq_7.ds__month - , subq_7.ds__quarter - , subq_7.ds__year - , subq_7.ds__extract_year - , subq_7.ds__extract_quarter - , subq_7.ds__extract_month - , subq_7.ds__extract_day - , subq_7.ds__extract_dow - , subq_7.ds__extract_doy - , subq_7.visit__ds__day - , subq_7.visit__ds__week - , subq_7.visit__ds__month - , subq_7.visit__ds__quarter - , subq_7.visit__ds__year - , subq_7.visit__ds__extract_year - , subq_7.visit__ds__extract_quarter - , subq_7.visit__ds__extract_month - , subq_7.visit__ds__extract_day - , subq_7.visit__ds__extract_dow - , subq_7.visit__ds__extract_doy - , subq_7.metric_time__day - , subq_7.metric_time__week - , subq_7.metric_time__month - , subq_7.metric_time__quarter - , subq_7.metric_time__year - , subq_7.metric_time__extract_year - , subq_7.metric_time__extract_quarter - , subq_7.metric_time__extract_month - , subq_7.metric_time__extract_day - , subq_7.metric_time__extract_dow - , subq_7.metric_time__extract_doy - , subq_7.user - , subq_7.session - , subq_7.visit__user - , subq_7.visit__session - , subq_7.referrer_id - , subq_7.visit__referrer_id - , subq_7.visits - , subq_7.visitors + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.visit__ds__day + , subq_8.visit__ds__week + , subq_8.visit__ds__month + , subq_8.visit__ds__quarter + , subq_8.visit__ds__year + , subq_8.visit__ds__extract_year + , subq_8.visit__ds__extract_quarter + , subq_8.visit__ds__extract_month + , subq_8.visit__ds__extract_day + , subq_8.visit__ds__extract_dow + , subq_8.visit__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session + , subq_8.visit__user + , subq_8.visit__session + , subq_8.referrer_id + , subq_8.visit__referrer_id + , subq_8.visits + , subq_8.visitors FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] SELECT - subq_6.ds__day - , subq_6.ds__week - , subq_6.ds__month - , subq_6.ds__quarter - , subq_6.ds__year - , subq_6.ds__extract_year - , subq_6.ds__extract_quarter - , subq_6.ds__extract_month - , subq_6.ds__extract_day - , subq_6.ds__extract_dow - , subq_6.ds__extract_doy - , subq_6.visit__ds__day - , subq_6.visit__ds__week - , subq_6.visit__ds__month - , subq_6.visit__ds__quarter - , subq_6.visit__ds__year - , subq_6.visit__ds__extract_year - , subq_6.visit__ds__extract_quarter - , subq_6.visit__ds__extract_month - , subq_6.visit__ds__extract_day - , subq_6.visit__ds__extract_dow - , subq_6.visit__ds__extract_doy - , subq_6.ds__day AS metric_time__day - , subq_6.ds__week AS metric_time__week - , subq_6.ds__month AS metric_time__month - , subq_6.ds__quarter AS metric_time__quarter - , subq_6.ds__year AS metric_time__year - , subq_6.ds__extract_year AS metric_time__extract_year - , subq_6.ds__extract_quarter AS metric_time__extract_quarter - , subq_6.ds__extract_month AS metric_time__extract_month - , subq_6.ds__extract_day AS metric_time__extract_day - , subq_6.ds__extract_dow AS metric_time__extract_dow - , subq_6.ds__extract_doy AS metric_time__extract_doy - , subq_6.user - , subq_6.session - , subq_6.visit__user - , subq_6.visit__session - , subq_6.referrer_id - , subq_6.visit__referrer_id - , subq_6.visits - , subq_6.visitors + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_6 - ) subq_7 - WHERE subq_7.metric_time__day BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_8 - ) subq_9 + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE subq_7.metric_time__day BETWEEN '2020-01-01' AND '2020-01-02' + ) subq_8 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_9 + ) subq_10 INNER JOIN ( -- Add column with generated UUID SELECT - subq_11.ds__day - , subq_11.ds__week - , subq_11.ds__month - , subq_11.ds__quarter - , subq_11.ds__year - , subq_11.ds__extract_year - , subq_11.ds__extract_quarter - , subq_11.ds__extract_month - , subq_11.ds__extract_day - , subq_11.ds__extract_dow - , subq_11.ds__extract_doy - , subq_11.buy__ds__day - , subq_11.buy__ds__week - , subq_11.buy__ds__month - , subq_11.buy__ds__quarter - , subq_11.buy__ds__year - , subq_11.buy__ds__extract_year - , subq_11.buy__ds__extract_quarter - , subq_11.buy__ds__extract_month - , subq_11.buy__ds__extract_day - , subq_11.buy__ds__extract_dow - , subq_11.buy__ds__extract_doy - , subq_11.metric_time__day - , subq_11.metric_time__week - , subq_11.metric_time__month - , subq_11.metric_time__quarter - , subq_11.metric_time__year - , subq_11.metric_time__extract_year - , subq_11.metric_time__extract_quarter - , subq_11.metric_time__extract_month - , subq_11.metric_time__extract_day - , subq_11.metric_time__extract_dow - , subq_11.metric_time__extract_doy - , subq_11.user - , subq_11.session_id - , subq_11.buy__user - , subq_11.buy__session_id - , subq_11.buys - , subq_11.buyers + subq_12.ds__day + , subq_12.ds__week + , subq_12.ds__month + , subq_12.ds__quarter + , subq_12.ds__year + , subq_12.ds__extract_year + , subq_12.ds__extract_quarter + , subq_12.ds__extract_month + , subq_12.ds__extract_day + , subq_12.ds__extract_dow + , subq_12.ds__extract_doy + , subq_12.buy__ds__day + , subq_12.buy__ds__week + , subq_12.buy__ds__month + , subq_12.buy__ds__quarter + , subq_12.buy__ds__year + , subq_12.buy__ds__extract_year + , subq_12.buy__ds__extract_quarter + , subq_12.buy__ds__extract_month + , subq_12.buy__ds__extract_day + , subq_12.buy__ds__extract_dow + , subq_12.buy__ds__extract_doy + , subq_12.metric_time__day + , subq_12.metric_time__week + , subq_12.metric_time__month + , subq_12.metric_time__quarter + , subq_12.metric_time__year + , subq_12.metric_time__extract_year + , subq_12.metric_time__extract_quarter + , subq_12.metric_time__extract_month + , subq_12.metric_time__extract_day + , subq_12.metric_time__extract_dow + , subq_12.metric_time__extract_doy + , subq_12.user + , subq_12.session_id + , subq_12.buy__user + , subq_12.buy__session_id + , subq_12.buys + , subq_12.buyers , UUID_STRING() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_10.ds__day - , subq_10.ds__week - , subq_10.ds__month - , subq_10.ds__quarter - , subq_10.ds__year - , subq_10.ds__extract_year - , subq_10.ds__extract_quarter - , subq_10.ds__extract_month - , subq_10.ds__extract_day - , subq_10.ds__extract_dow - , subq_10.ds__extract_doy - , subq_10.buy__ds__day - , subq_10.buy__ds__week - , subq_10.buy__ds__month - , subq_10.buy__ds__quarter - , subq_10.buy__ds__year - , subq_10.buy__ds__extract_year - , subq_10.buy__ds__extract_quarter - , subq_10.buy__ds__extract_month - , subq_10.buy__ds__extract_day - , subq_10.buy__ds__extract_dow - , subq_10.buy__ds__extract_doy - , subq_10.ds__day AS metric_time__day - , subq_10.ds__week AS metric_time__week - , subq_10.ds__month AS metric_time__month - , subq_10.ds__quarter AS metric_time__quarter - , subq_10.ds__year AS metric_time__year - , subq_10.ds__extract_year AS metric_time__extract_year - , subq_10.ds__extract_quarter AS metric_time__extract_quarter - , subq_10.ds__extract_month AS metric_time__extract_month - , subq_10.ds__extract_day AS metric_time__extract_day - , subq_10.ds__extract_dow AS metric_time__extract_dow - , subq_10.ds__extract_doy AS metric_time__extract_doy - , subq_10.user - , subq_10.session_id - , subq_10.buy__user - , subq_10.buy__session_id - , subq_10.buys - , subq_10.buyers + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.ds__day AS metric_time__day + , subq_11.ds__week AS metric_time__week + , subq_11.ds__month AS metric_time__month + , subq_11.ds__quarter AS metric_time__quarter + , subq_11.ds__year AS metric_time__year + , subq_11.ds__extract_year AS metric_time__extract_year + , subq_11.ds__extract_quarter AS metric_time__extract_quarter + , subq_11.ds__extract_month AS metric_time__extract_month + , subq_11.ds__extract_day AS metric_time__extract_day + , subq_11.ds__extract_dow AS metric_time__extract_dow + , subq_11.ds__extract_doy AS metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -479,33 +525,33 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_10 - ) subq_11 - ) subq_12 + ) subq_11 + ) subq_12 + ) subq_13 ON ( - subq_9.user = subq_12.user + subq_10.user = subq_13.user ) AND ( ( - subq_9.ds__day <= subq_12.ds__day + subq_10.ds__day <= subq_13.ds__day ) AND ( - subq_9.ds__day > DATEADD(day, -7, subq_12.ds__day) + subq_10.ds__day > DATEADD(day, -7, subq_13.ds__day) ) ) - ) subq_13 - ) subq_14 - ) subq_15 + ) subq_14 + ) subq_15 + ) subq_16 GROUP BY - subq_15.metric_time__day - , subq_15.visit__referrer_id - ) subq_16 + subq_16.metric_time__day + , subq_16.visit__referrer_id + ) subq_17 ON ( - subq_5.visit__referrer_id = subq_16.visit__referrer_id + subq_5.visit__referrer_id = subq_17.visit__referrer_id ) AND ( - subq_5.metric_time__day = subq_16.metric_time__day + subq_5.metric_time__day = subq_17.metric_time__day ) GROUP BY - COALESCE(subq_5.metric_time__day, subq_16.metric_time__day) - , COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) -) subq_17 + COALESCE(subq_5.metric_time__day, subq_17.metric_time__day) + , COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) +) subq_18 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql index 402c6443d7..aefaf3f621 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql @@ -6,10 +6,10 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_23.metric_time__day, subq_34.metric_time__day) AS metric_time__day - , COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) AS visit__referrer_id - , MAX(subq_23.visits) AS visits - , MAX(subq_34.buys) AS buys + COALESCE(subq_24.metric_time__day, subq_36.metric_time__day) AS metric_time__day + , COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) AS visit__referrer_id + , MAX(subq_24.visits) AS visits + , MAX(subq_36.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -28,12 +28,12 @@ FROM ( , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_21 + ) subq_22 WHERE visit__referrer_id = 'ref_id_01' GROUP BY metric_time__day , visit__referrer_id - ) subq_23 + ) subq_24 FULL OUTER JOIN ( -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] @@ -45,62 +45,72 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_27.visits) OVER ( + FIRST_VALUE(subq_29.visits) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_27.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_29.visit__referrer_id) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_27.ds__day) OVER ( + , FIRST_VALUE(subq_29.ds__day) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_27.metric_time__day) OVER ( + , FIRST_VALUE(subq_29.metric_time__day) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_27.user) OVER ( + , FIRST_VALUE(subq_29.user) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_30.mf_internal_uuid AS mf_internal_uuid - , subq_30.buys AS buys + , subq_32.mf_internal_uuid AS mf_internal_uuid + , subq_32.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_27 + ds__day + , metric_time__day + , subq_27.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' + ) subq_27 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_29 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -111,29 +121,29 @@ FROM ( , 1 AS buys , UUID_STRING() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_30 + ) subq_32 ON ( - subq_27.user = subq_30.user + subq_29.user = subq_32.user ) AND ( ( - subq_27.ds__day <= subq_30.ds__day + subq_29.ds__day <= subq_32.ds__day ) AND ( - subq_27.ds__day > DATEADD(day, -7, subq_30.ds__day) + subq_29.ds__day > DATEADD(day, -7, subq_32.ds__day) ) ) - ) subq_31 + ) subq_33 GROUP BY metric_time__day , visit__referrer_id - ) subq_34 + ) subq_36 ON ( - subq_23.visit__referrer_id = subq_34.visit__referrer_id + subq_24.visit__referrer_id = subq_36.visit__referrer_id ) AND ( - subq_23.metric_time__day = subq_34.metric_time__day + subq_24.metric_time__day = subq_36.metric_time__day ) GROUP BY - COALESCE(subq_23.metric_time__day, subq_34.metric_time__day) - , COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) -) subq_35 + COALESCE(subq_24.metric_time__day, subq_36.metric_time__day) + , COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) +) subq_37 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric__plan0.sql index b00bd24e15..561f039d14 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric__plan0.sql @@ -1,13 +1,13 @@ -- Compute Metrics via Expressions SELECT - subq_15.metric_time__day - , CAST(subq_15.buys AS DOUBLE) / CAST(NULLIF(subq_15.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate + subq_16.metric_time__day + , CAST(subq_16.buys AS DOUBLE) / CAST(NULLIF(subq_16.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) AS metric_time__day + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) AS metric_time__day , MAX(subq_4.visits) AS visits - , MAX(subq_14.buys) AS buys + , MAX(subq_15.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -112,231 +112,277 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_13.metric_time__day - , SUM(subq_13.buys) AS buys + subq_14.metric_time__day + , SUM(subq_14.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'metric_time__day'] SELECT - subq_12.metric_time__day - , subq_12.buys + subq_13.metric_time__day + , subq_13.buys FROM ( -- Find conversions for user within the range of INF SELECT - subq_11.ds__day - , subq_11.metric_time__day - , subq_11.user - , subq_11.buys - , subq_11.visits + subq_12.ds__day + , subq_12.metric_time__day + , subq_12.user + , subq_12.buys + , subq_12.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_7.visits) OVER ( + FIRST_VALUE(subq_8.visits) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_7.ds__day) OVER ( + , FIRST_VALUE(subq_8.ds__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_7.metric_time__day) OVER ( + , FIRST_VALUE(subq_8.metric_time__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_7.user) OVER ( + , FIRST_VALUE(subq_8.user) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_10.mf_internal_uuid AS mf_internal_uuid - , subq_10.buys AS buys + , subq_11.mf_internal_uuid AS mf_internal_uuid + , subq_11.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_6.ds__day - , subq_6.metric_time__day - , subq_6.user - , subq_6.visits + subq_7.ds__day + , subq_7.metric_time__day + , subq_7.user + , subq_7.visits FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE SELECT - subq_5.ds__day - , subq_5.ds__week - , subq_5.ds__month - , subq_5.ds__quarter - , subq_5.ds__year - , subq_5.ds__extract_year - , subq_5.ds__extract_quarter - , subq_5.ds__extract_month - , subq_5.ds__extract_day - , subq_5.ds__extract_dow - , subq_5.ds__extract_doy - , subq_5.visit__ds__day - , subq_5.visit__ds__week - , subq_5.visit__ds__month - , subq_5.visit__ds__quarter - , subq_5.visit__ds__year - , subq_5.visit__ds__extract_year - , subq_5.visit__ds__extract_quarter - , subq_5.visit__ds__extract_month - , subq_5.visit__ds__extract_day - , subq_5.visit__ds__extract_dow - , subq_5.visit__ds__extract_doy - , subq_5.ds__day AS metric_time__day - , subq_5.ds__week AS metric_time__week - , subq_5.ds__month AS metric_time__month - , subq_5.ds__quarter AS metric_time__quarter - , subq_5.ds__year AS metric_time__year - , subq_5.ds__extract_year AS metric_time__extract_year - , subq_5.ds__extract_quarter AS metric_time__extract_quarter - , subq_5.ds__extract_month AS metric_time__extract_month - , subq_5.ds__extract_day AS metric_time__extract_day - , subq_5.ds__extract_dow AS metric_time__extract_dow - , subq_5.ds__extract_doy AS metric_time__extract_doy - , subq_5.user - , subq_5.session - , subq_5.visit__user - , subq_5.visit__session - , subq_5.referrer_id - , subq_5.visit__referrer_id - , subq_5.visits - , subq_5.visitors + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.metric_time__day + , subq_6.metric_time__week + , subq_6.metric_time__month + , subq_6.metric_time__quarter + , subq_6.metric_time__year + , subq_6.metric_time__extract_year + , subq_6.metric_time__extract_quarter + , subq_6.metric_time__extract_month + , subq_6.metric_time__extract_day + , subq_6.metric_time__extract_dow + , subq_6.metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_5 - ) subq_6 - ) subq_7 + subq_5.ds__day + , subq_5.ds__week + , subq_5.ds__month + , subq_5.ds__quarter + , subq_5.ds__year + , subq_5.ds__extract_year + , subq_5.ds__extract_quarter + , subq_5.ds__extract_month + , subq_5.ds__extract_day + , subq_5.ds__extract_dow + , subq_5.ds__extract_doy + , subq_5.visit__ds__day + , subq_5.visit__ds__week + , subq_5.visit__ds__month + , subq_5.visit__ds__quarter + , subq_5.visit__ds__year + , subq_5.visit__ds__extract_year + , subq_5.visit__ds__extract_quarter + , subq_5.visit__ds__extract_month + , subq_5.visit__ds__extract_day + , subq_5.visit__ds__extract_dow + , subq_5.visit__ds__extract_doy + , subq_5.ds__day AS metric_time__day + , subq_5.ds__week AS metric_time__week + , subq_5.ds__month AS metric_time__month + , subq_5.ds__quarter AS metric_time__quarter + , subq_5.ds__year AS metric_time__year + , subq_5.ds__extract_year AS metric_time__extract_year + , subq_5.ds__extract_quarter AS metric_time__extract_quarter + , subq_5.ds__extract_month AS metric_time__extract_month + , subq_5.ds__extract_day AS metric_time__extract_day + , subq_5.ds__extract_dow AS metric_time__extract_dow + , subq_5.ds__extract_doy AS metric_time__extract_doy + , subq_5.user + , subq_5.session + , subq_5.visit__user + , subq_5.visit__session + , subq_5.referrer_id + , subq_5.visit__referrer_id + , subq_5.visits + , subq_5.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_5 + ) subq_6 + WHERE metric_time__day = '2020-01-01' + ) subq_7 + ) subq_8 INNER JOIN ( -- Add column with generated UUID SELECT - subq_9.ds__day - , subq_9.ds__week - , subq_9.ds__month - , subq_9.ds__quarter - , subq_9.ds__year - , subq_9.ds__extract_year - , subq_9.ds__extract_quarter - , subq_9.ds__extract_month - , subq_9.ds__extract_day - , subq_9.ds__extract_dow - , subq_9.ds__extract_doy - , subq_9.buy__ds__day - , subq_9.buy__ds__week - , subq_9.buy__ds__month - , subq_9.buy__ds__quarter - , subq_9.buy__ds__year - , subq_9.buy__ds__extract_year - , subq_9.buy__ds__extract_quarter - , subq_9.buy__ds__extract_month - , subq_9.buy__ds__extract_day - , subq_9.buy__ds__extract_dow - , subq_9.buy__ds__extract_doy - , subq_9.metric_time__day - , subq_9.metric_time__week - , subq_9.metric_time__month - , subq_9.metric_time__quarter - , subq_9.metric_time__year - , subq_9.metric_time__extract_year - , subq_9.metric_time__extract_quarter - , subq_9.metric_time__extract_month - , subq_9.metric_time__extract_day - , subq_9.metric_time__extract_dow - , subq_9.metric_time__extract_doy - , subq_9.user - , subq_9.session_id - , subq_9.buy__user - , subq_9.buy__session_id - , subq_9.buys - , subq_9.buyers + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.metric_time__day + , subq_10.metric_time__week + , subq_10.metric_time__month + , subq_10.metric_time__quarter + , subq_10.metric_time__year + , subq_10.metric_time__extract_year + , subq_10.metric_time__extract_quarter + , subq_10.metric_time__extract_month + , subq_10.metric_time__extract_day + , subq_10.metric_time__extract_dow + , subq_10.metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers , uuid() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_8.ds__day - , subq_8.ds__week - , subq_8.ds__month - , subq_8.ds__quarter - , subq_8.ds__year - , subq_8.ds__extract_year - , subq_8.ds__extract_quarter - , subq_8.ds__extract_month - , subq_8.ds__extract_day - , subq_8.ds__extract_dow - , subq_8.ds__extract_doy - , subq_8.buy__ds__day - , subq_8.buy__ds__week - , subq_8.buy__ds__month - , subq_8.buy__ds__quarter - , subq_8.buy__ds__year - , subq_8.buy__ds__extract_year - , subq_8.buy__ds__extract_quarter - , subq_8.buy__ds__extract_month - , subq_8.buy__ds__extract_day - , subq_8.buy__ds__extract_dow - , subq_8.buy__ds__extract_doy - , subq_8.ds__day AS metric_time__day - , subq_8.ds__week AS metric_time__week - , subq_8.ds__month AS metric_time__month - , subq_8.ds__quarter AS metric_time__quarter - , subq_8.ds__year AS metric_time__year - , subq_8.ds__extract_year AS metric_time__extract_year - , subq_8.ds__extract_quarter AS metric_time__extract_quarter - , subq_8.ds__extract_month AS metric_time__extract_month - , subq_8.ds__extract_day AS metric_time__extract_day - , subq_8.ds__extract_dow AS metric_time__extract_dow - , subq_8.ds__extract_doy AS metric_time__extract_doy - , subq_8.user - , subq_8.session_id - , subq_8.buy__user - , subq_8.buy__session_id - , subq_8.buys - , subq_8.buyers + subq_9.ds__day + , subq_9.ds__week + , subq_9.ds__month + , subq_9.ds__quarter + , subq_9.ds__year + , subq_9.ds__extract_year + , subq_9.ds__extract_quarter + , subq_9.ds__extract_month + , subq_9.ds__extract_day + , subq_9.ds__extract_dow + , subq_9.ds__extract_doy + , subq_9.buy__ds__day + , subq_9.buy__ds__week + , subq_9.buy__ds__month + , subq_9.buy__ds__quarter + , subq_9.buy__ds__year + , subq_9.buy__ds__extract_year + , subq_9.buy__ds__extract_quarter + , subq_9.buy__ds__extract_month + , subq_9.buy__ds__extract_day + , subq_9.buy__ds__extract_dow + , subq_9.buy__ds__extract_doy + , subq_9.ds__day AS metric_time__day + , subq_9.ds__week AS metric_time__week + , subq_9.ds__month AS metric_time__month + , subq_9.ds__quarter AS metric_time__quarter + , subq_9.ds__year AS metric_time__year + , subq_9.ds__extract_year AS metric_time__extract_year + , subq_9.ds__extract_quarter AS metric_time__extract_quarter + , subq_9.ds__extract_month AS metric_time__extract_month + , subq_9.ds__extract_day AS metric_time__extract_day + , subq_9.ds__extract_dow AS metric_time__extract_dow + , subq_9.ds__extract_doy AS metric_time__extract_doy + , subq_9.user + , subq_9.session_id + , subq_9.buy__user + , subq_9.buy__session_id + , subq_9.buys + , subq_9.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -369,23 +415,23 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_8 - ) subq_9 - ) subq_10 + ) subq_9 + ) subq_10 + ) subq_11 ON ( - subq_7.user = subq_10.user + subq_8.user = subq_11.user ) AND ( - (subq_7.ds__day <= subq_10.ds__day) + (subq_8.ds__day <= subq_11.ds__day) ) - ) subq_11 - ) subq_12 - ) subq_13 + ) subq_12 + ) subq_13 + ) subq_14 GROUP BY - subq_13.metric_time__day - ) subq_14 + subq_14.metric_time__day + ) subq_15 ON - subq_4.metric_time__day = subq_14.metric_time__day + subq_4.metric_time__day = subq_15.metric_time__day GROUP BY - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) -) subq_15 + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) +) subq_16 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric__plan0_optimized.sql index 9fe6b6c32f..f2da3473a3 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric__plan0_optimized.sql @@ -5,9 +5,9 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_20.visits) AS visits - , MAX(subq_30.buys) AS buys + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , MAX(subq_21.visits) AS visits + , MAX(subq_32.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -22,11 +22,11 @@ FROM ( DATE_TRUNC('day', ds) AS metric_time__day , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_18 + ) subq_19 WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__day - ) subq_20 + ) subq_21 FULL OUTER JOIN ( -- Find conversions for user within the range of INF -- Pass Only Elements: ['buys', 'metric_time__day'] @@ -37,51 +37,60 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_23.visits) OVER ( + FIRST_VALUE(subq_25.visits) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_23.ds__day) OVER ( + , FIRST_VALUE(subq_25.ds__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_23.metric_time__day) OVER ( + , FIRST_VALUE(subq_25.metric_time__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_23.user) OVER ( + , FIRST_VALUE(subq_25.user) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_26.mf_internal_uuid AS mf_internal_uuid - , subq_26.buys AS buys + , subq_28.mf_internal_uuid AS mf_internal_uuid + , subq_28.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + ds__day + , metric_time__day + , subq_23.user + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_23 + WHERE metric_time__day = '2020-01-01' + ) subq_25 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -92,19 +101,19 @@ FROM ( , 1 AS buys , uuid() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_26 + ) subq_28 ON ( - subq_23.user = subq_26.user + subq_25.user = subq_28.user ) AND ( - (subq_23.ds__day <= subq_26.ds__day) + (subq_25.ds__day <= subq_28.ds__day) ) - ) subq_27 + ) subq_29 GROUP BY metric_time__day - ) subq_30 + ) subq_32 ON - subq_20.metric_time__day = subq_30.metric_time__day + subq_21.metric_time__day = subq_32.metric_time__day GROUP BY - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) -) subq_31 + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) +) subq_33 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_categorical_filter__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_categorical_filter__plan0.sql index 32ba655478..25040c1169 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_categorical_filter__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_categorical_filter__plan0.sql @@ -1,15 +1,15 @@ -- Compute Metrics via Expressions SELECT - subq_15.metric_time__day - , subq_15.visit__referrer_id - , CAST(subq_15.buys AS DOUBLE) / CAST(NULLIF(subq_15.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate + subq_16.metric_time__day + , subq_16.visit__referrer_id + , CAST(subq_16.buys AS DOUBLE) / CAST(NULLIF(subq_16.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) AS metric_time__day - , COALESCE(subq_4.visit__referrer_id, subq_14.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) AS metric_time__day + , COALESCE(subq_4.visit__referrer_id, subq_15.visit__referrer_id) AS visit__referrer_id , MAX(subq_4.visits) AS visits - , MAX(subq_14.buys) AS buys + , MAX(subq_15.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -118,243 +118,289 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_13.metric_time__day - , subq_13.visit__referrer_id - , SUM(subq_13.buys) AS buys + subq_14.metric_time__day + , subq_14.visit__referrer_id + , SUM(subq_14.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] SELECT - subq_12.metric_time__day - , subq_12.visit__referrer_id - , subq_12.buys + subq_13.metric_time__day + , subq_13.visit__referrer_id + , subq_13.buys FROM ( -- Find conversions for user within the range of INF SELECT - subq_11.ds__day - , subq_11.metric_time__day - , subq_11.user - , subq_11.visit__referrer_id - , subq_11.buys - , subq_11.visits + subq_12.ds__day + , subq_12.metric_time__day + , subq_12.user + , subq_12.visit__referrer_id + , subq_12.buys + , subq_12.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_7.visits) OVER ( + FIRST_VALUE(subq_8.visits) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_7.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_8.visit__referrer_id) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_7.ds__day) OVER ( + , FIRST_VALUE(subq_8.ds__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_7.metric_time__day) OVER ( + , FIRST_VALUE(subq_8.metric_time__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_7.user) OVER ( + , FIRST_VALUE(subq_8.user) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_10.mf_internal_uuid AS mf_internal_uuid - , subq_10.buys AS buys + , subq_11.mf_internal_uuid AS mf_internal_uuid + , subq_11.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_6.ds__day - , subq_6.metric_time__day - , subq_6.user - , subq_6.visit__referrer_id - , subq_6.visits + subq_7.ds__day + , subq_7.metric_time__day + , subq_7.user + , subq_7.visit__referrer_id + , subq_7.visits FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE SELECT - subq_5.ds__day - , subq_5.ds__week - , subq_5.ds__month - , subq_5.ds__quarter - , subq_5.ds__year - , subq_5.ds__extract_year - , subq_5.ds__extract_quarter - , subq_5.ds__extract_month - , subq_5.ds__extract_day - , subq_5.ds__extract_dow - , subq_5.ds__extract_doy - , subq_5.visit__ds__day - , subq_5.visit__ds__week - , subq_5.visit__ds__month - , subq_5.visit__ds__quarter - , subq_5.visit__ds__year - , subq_5.visit__ds__extract_year - , subq_5.visit__ds__extract_quarter - , subq_5.visit__ds__extract_month - , subq_5.visit__ds__extract_day - , subq_5.visit__ds__extract_dow - , subq_5.visit__ds__extract_doy - , subq_5.ds__day AS metric_time__day - , subq_5.ds__week AS metric_time__week - , subq_5.ds__month AS metric_time__month - , subq_5.ds__quarter AS metric_time__quarter - , subq_5.ds__year AS metric_time__year - , subq_5.ds__extract_year AS metric_time__extract_year - , subq_5.ds__extract_quarter AS metric_time__extract_quarter - , subq_5.ds__extract_month AS metric_time__extract_month - , subq_5.ds__extract_day AS metric_time__extract_day - , subq_5.ds__extract_dow AS metric_time__extract_dow - , subq_5.ds__extract_doy AS metric_time__extract_doy - , subq_5.user - , subq_5.session - , subq_5.visit__user - , subq_5.visit__session - , subq_5.referrer_id - , subq_5.visit__referrer_id - , subq_5.visits - , subq_5.visitors + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.metric_time__day + , subq_6.metric_time__week + , subq_6.metric_time__month + , subq_6.metric_time__quarter + , subq_6.metric_time__year + , subq_6.metric_time__extract_year + , subq_6.metric_time__extract_quarter + , subq_6.metric_time__extract_month + , subq_6.metric_time__extract_day + , subq_6.metric_time__extract_dow + , subq_6.metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_5 - ) subq_6 - ) subq_7 + subq_5.ds__day + , subq_5.ds__week + , subq_5.ds__month + , subq_5.ds__quarter + , subq_5.ds__year + , subq_5.ds__extract_year + , subq_5.ds__extract_quarter + , subq_5.ds__extract_month + , subq_5.ds__extract_day + , subq_5.ds__extract_dow + , subq_5.ds__extract_doy + , subq_5.visit__ds__day + , subq_5.visit__ds__week + , subq_5.visit__ds__month + , subq_5.visit__ds__quarter + , subq_5.visit__ds__year + , subq_5.visit__ds__extract_year + , subq_5.visit__ds__extract_quarter + , subq_5.visit__ds__extract_month + , subq_5.visit__ds__extract_day + , subq_5.visit__ds__extract_dow + , subq_5.visit__ds__extract_doy + , subq_5.ds__day AS metric_time__day + , subq_5.ds__week AS metric_time__week + , subq_5.ds__month AS metric_time__month + , subq_5.ds__quarter AS metric_time__quarter + , subq_5.ds__year AS metric_time__year + , subq_5.ds__extract_year AS metric_time__extract_year + , subq_5.ds__extract_quarter AS metric_time__extract_quarter + , subq_5.ds__extract_month AS metric_time__extract_month + , subq_5.ds__extract_day AS metric_time__extract_day + , subq_5.ds__extract_dow AS metric_time__extract_dow + , subq_5.ds__extract_doy AS metric_time__extract_doy + , subq_5.user + , subq_5.session + , subq_5.visit__user + , subq_5.visit__session + , subq_5.referrer_id + , subq_5.visit__referrer_id + , subq_5.visits + , subq_5.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_5 + ) subq_6 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_7 + ) subq_8 INNER JOIN ( -- Add column with generated UUID SELECT - subq_9.ds__day - , subq_9.ds__week - , subq_9.ds__month - , subq_9.ds__quarter - , subq_9.ds__year - , subq_9.ds__extract_year - , subq_9.ds__extract_quarter - , subq_9.ds__extract_month - , subq_9.ds__extract_day - , subq_9.ds__extract_dow - , subq_9.ds__extract_doy - , subq_9.buy__ds__day - , subq_9.buy__ds__week - , subq_9.buy__ds__month - , subq_9.buy__ds__quarter - , subq_9.buy__ds__year - , subq_9.buy__ds__extract_year - , subq_9.buy__ds__extract_quarter - , subq_9.buy__ds__extract_month - , subq_9.buy__ds__extract_day - , subq_9.buy__ds__extract_dow - , subq_9.buy__ds__extract_doy - , subq_9.metric_time__day - , subq_9.metric_time__week - , subq_9.metric_time__month - , subq_9.metric_time__quarter - , subq_9.metric_time__year - , subq_9.metric_time__extract_year - , subq_9.metric_time__extract_quarter - , subq_9.metric_time__extract_month - , subq_9.metric_time__extract_day - , subq_9.metric_time__extract_dow - , subq_9.metric_time__extract_doy - , subq_9.user - , subq_9.session_id - , subq_9.buy__user - , subq_9.buy__session_id - , subq_9.buys - , subq_9.buyers + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.metric_time__day + , subq_10.metric_time__week + , subq_10.metric_time__month + , subq_10.metric_time__quarter + , subq_10.metric_time__year + , subq_10.metric_time__extract_year + , subq_10.metric_time__extract_quarter + , subq_10.metric_time__extract_month + , subq_10.metric_time__extract_day + , subq_10.metric_time__extract_dow + , subq_10.metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers , uuid() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_8.ds__day - , subq_8.ds__week - , subq_8.ds__month - , subq_8.ds__quarter - , subq_8.ds__year - , subq_8.ds__extract_year - , subq_8.ds__extract_quarter - , subq_8.ds__extract_month - , subq_8.ds__extract_day - , subq_8.ds__extract_dow - , subq_8.ds__extract_doy - , subq_8.buy__ds__day - , subq_8.buy__ds__week - , subq_8.buy__ds__month - , subq_8.buy__ds__quarter - , subq_8.buy__ds__year - , subq_8.buy__ds__extract_year - , subq_8.buy__ds__extract_quarter - , subq_8.buy__ds__extract_month - , subq_8.buy__ds__extract_day - , subq_8.buy__ds__extract_dow - , subq_8.buy__ds__extract_doy - , subq_8.ds__day AS metric_time__day - , subq_8.ds__week AS metric_time__week - , subq_8.ds__month AS metric_time__month - , subq_8.ds__quarter AS metric_time__quarter - , subq_8.ds__year AS metric_time__year - , subq_8.ds__extract_year AS metric_time__extract_year - , subq_8.ds__extract_quarter AS metric_time__extract_quarter - , subq_8.ds__extract_month AS metric_time__extract_month - , subq_8.ds__extract_day AS metric_time__extract_day - , subq_8.ds__extract_dow AS metric_time__extract_dow - , subq_8.ds__extract_doy AS metric_time__extract_doy - , subq_8.user - , subq_8.session_id - , subq_8.buy__user - , subq_8.buy__session_id - , subq_8.buys - , subq_8.buyers + subq_9.ds__day + , subq_9.ds__week + , subq_9.ds__month + , subq_9.ds__quarter + , subq_9.ds__year + , subq_9.ds__extract_year + , subq_9.ds__extract_quarter + , subq_9.ds__extract_month + , subq_9.ds__extract_day + , subq_9.ds__extract_dow + , subq_9.ds__extract_doy + , subq_9.buy__ds__day + , subq_9.buy__ds__week + , subq_9.buy__ds__month + , subq_9.buy__ds__quarter + , subq_9.buy__ds__year + , subq_9.buy__ds__extract_year + , subq_9.buy__ds__extract_quarter + , subq_9.buy__ds__extract_month + , subq_9.buy__ds__extract_day + , subq_9.buy__ds__extract_dow + , subq_9.buy__ds__extract_doy + , subq_9.ds__day AS metric_time__day + , subq_9.ds__week AS metric_time__week + , subq_9.ds__month AS metric_time__month + , subq_9.ds__quarter AS metric_time__quarter + , subq_9.ds__year AS metric_time__year + , subq_9.ds__extract_year AS metric_time__extract_year + , subq_9.ds__extract_quarter AS metric_time__extract_quarter + , subq_9.ds__extract_month AS metric_time__extract_month + , subq_9.ds__extract_day AS metric_time__extract_day + , subq_9.ds__extract_dow AS metric_time__extract_dow + , subq_9.ds__extract_doy AS metric_time__extract_doy + , subq_9.user + , subq_9.session_id + , subq_9.buy__user + , subq_9.buy__session_id + , subq_9.buys + , subq_9.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -387,29 +433,29 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_8 - ) subq_9 - ) subq_10 + ) subq_9 + ) subq_10 + ) subq_11 ON ( - subq_7.user = subq_10.user + subq_8.user = subq_11.user ) AND ( - (subq_7.ds__day <= subq_10.ds__day) + (subq_8.ds__day <= subq_11.ds__day) ) - ) subq_11 - ) subq_12 - ) subq_13 + ) subq_12 + ) subq_13 + ) subq_14 GROUP BY - subq_13.metric_time__day - , subq_13.visit__referrer_id - ) subq_14 + subq_14.metric_time__day + , subq_14.visit__referrer_id + ) subq_15 ON ( - subq_4.visit__referrer_id = subq_14.visit__referrer_id + subq_4.visit__referrer_id = subq_15.visit__referrer_id ) AND ( - subq_4.metric_time__day = subq_14.metric_time__day + subq_4.metric_time__day = subq_15.metric_time__day ) GROUP BY - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) - , COALESCE(subq_4.visit__referrer_id, subq_14.visit__referrer_id) -) subq_15 + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) + , COALESCE(subq_4.visit__referrer_id, subq_15.visit__referrer_id) +) subq_16 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_categorical_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_categorical_filter__plan0_optimized.sql index 4485f78bd4..7a956b7fbc 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_categorical_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_categorical_filter__plan0_optimized.sql @@ -6,10 +6,10 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , COALESCE(subq_20.visit__referrer_id, subq_30.visit__referrer_id) AS visit__referrer_id - , MAX(subq_20.visits) AS visits - , MAX(subq_30.buys) AS buys + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , COALESCE(subq_21.visit__referrer_id, subq_32.visit__referrer_id) AS visit__referrer_id + , MAX(subq_21.visits) AS visits + , MAX(subq_32.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -26,12 +26,12 @@ FROM ( , referrer_id AS visit__referrer_id , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_18 + ) subq_19 WHERE visit__referrer_id = 'ref_id_01' GROUP BY metric_time__day , visit__referrer_id - ) subq_20 + ) subq_21 FULL OUTER JOIN ( -- Find conversions for user within the range of INF -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] @@ -43,60 +43,70 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_23.visits) OVER ( + FIRST_VALUE(subq_25.visits) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_23.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_25.visit__referrer_id) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_23.ds__day) OVER ( + , FIRST_VALUE(subq_25.ds__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_23.metric_time__day) OVER ( + , FIRST_VALUE(subq_25.metric_time__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_23.user) OVER ( + , FIRST_VALUE(subq_25.user) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_26.mf_internal_uuid AS mf_internal_uuid - , subq_26.buys AS buys + , subq_28.mf_internal_uuid AS mf_internal_uuid + , subq_28.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + ds__day + , metric_time__day + , subq_23.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_23 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_25 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -107,25 +117,25 @@ FROM ( , 1 AS buys , uuid() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_26 + ) subq_28 ON ( - subq_23.user = subq_26.user + subq_25.user = subq_28.user ) AND ( - (subq_23.ds__day <= subq_26.ds__day) + (subq_25.ds__day <= subq_28.ds__day) ) - ) subq_27 + ) subq_29 GROUP BY metric_time__day , visit__referrer_id - ) subq_30 + ) subq_32 ON ( - subq_20.visit__referrer_id = subq_30.visit__referrer_id + subq_21.visit__referrer_id = subq_32.visit__referrer_id ) AND ( - subq_20.metric_time__day = subq_30.metric_time__day + subq_21.metric_time__day = subq_32.metric_time__day ) GROUP BY - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) - , COALESCE(subq_20.visit__referrer_id, subq_30.visit__referrer_id) -) subq_31 + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) + , COALESCE(subq_21.visit__referrer_id, subq_32.visit__referrer_id) +) subq_33 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_filter__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_filter__plan0.sql new file mode 100644 index 0000000000..95a06b0fb7 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_filter__plan0.sql @@ -0,0 +1,429 @@ +-- Compute Metrics via Expressions +SELECT + CAST(subq_17.buys AS DOUBLE) / CAST(NULLIF(subq_17.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_5.visits) AS visits + , MAX(subq_16.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_4.visits) AS visits + FROM ( + -- Pass Only Elements: ['visits',] + SELECT + subq_3.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_2.metric_time__day + , subq_2.visits + FROM ( + -- Pass Only Elements: ['visits', 'metric_time__day'] + SELECT + subq_1.metric_time__day + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_0 + ) subq_1 + ) subq_2 + WHERE metric_time__day = '2020-01-01' + ) subq_3 + ) subq_4 + ) subq_5 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_15.buys) AS buys + FROM ( + -- Pass Only Elements: ['buys',] + SELECT + subq_14.buys + FROM ( + -- Find conversions for user within the range of INF + SELECT + subq_13.ds__day + , subq_13.metric_time__day + , subq_13.user + , subq_13.buys + , subq_13.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_9.visits) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_9.ds__day) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_9.metric_time__day) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS metric_time__day + , FIRST_VALUE(subq_9.user) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_12.mf_internal_uuid AS mf_internal_uuid + , subq_12.buys AS buys + FROM ( + -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] + SELECT + subq_8.ds__day + , subq_8.metric_time__day + , subq_8.user + , subq_8.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE metric_time__day = '2020-01-01' + ) subq_8 + ) subq_9 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.metric_time__day + , subq_11.metric_time__week + , subq_11.metric_time__month + , subq_11.metric_time__quarter + , subq_11.metric_time__year + , subq_11.metric_time__extract_year + , subq_11.metric_time__extract_quarter + , subq_11.metric_time__extract_month + , subq_11.metric_time__extract_day + , subq_11.metric_time__extract_dow + , subq_11.metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers + , uuid() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.ds__day AS metric_time__day + , subq_10.ds__week AS metric_time__week + , subq_10.ds__month AS metric_time__month + , subq_10.ds__quarter AS metric_time__quarter + , subq_10.ds__year AS metric_time__year + , subq_10.ds__extract_year AS metric_time__extract_year + , subq_10.ds__extract_quarter AS metric_time__extract_quarter + , subq_10.ds__extract_month AS metric_time__extract_month + , subq_10.ds__extract_day AS metric_time__extract_day + , subq_10.ds__extract_dow AS metric_time__extract_dow + , subq_10.ds__extract_doy AS metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_28000.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM buys_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_28000.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM buys_source_src_28000.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS buy__ds__extract_doy + , buys_source_src_28000.user_id AS user + , buys_source_src_28000.session_id + , buys_source_src_28000.user_id AS buy__user + , buys_source_src_28000.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_10 + ) subq_11 + ) subq_12 + ON + ( + subq_9.user = subq_12.user + ) AND ( + (subq_9.ds__day <= subq_12.ds__day) + ) + ) subq_13 + ) subq_14 + ) subq_15 + ) subq_16 +) subq_17 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_filter__plan0_optimized.sql new file mode 100644 index 0000000000..8fa35e2087 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_filter__plan0_optimized.sql @@ -0,0 +1,103 @@ +-- Combine Aggregated Outputs +-- Compute Metrics via Expressions +SELECT + CAST(MAX(subq_34.buys) AS DOUBLE) / CAST(NULLIF(MAX(subq_23.visits), 0) AS DOUBLE) AS visit_buy_conversion_rate +FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits',] + -- Aggregate Measures + SELECT + 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_20 + WHERE metric_time__day = '2020-01-01' +) subq_23 +CROSS JOIN ( + -- Find conversions for user within the range of INF + -- Pass Only Elements: ['buys',] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_27.visits) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_27.ds__day) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_27.metric_time__day) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS metric_time__day + , FIRST_VALUE(subq_27.user) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_30.mf_internal_uuid AS mf_internal_uuid + , subq_30.buys AS buys + FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] + SELECT + ds__day + , metric_time__day + , subq_25.user + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_25 + WHERE metric_time__day = '2020-01-01' + ) subq_27 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , uuid() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_30 + ON + ( + subq_27.user = subq_30.user + ) AND ( + (subq_27.ds__day <= subq_30.ds__day) + ) + ) subq_31 +) subq_34 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_filter_not_in_group_by__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_filter_not_in_group_by__plan0.sql new file mode 100644 index 0000000000..83e8767658 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_filter_not_in_group_by__plan0.sql @@ -0,0 +1,433 @@ +-- Compute Metrics via Expressions +SELECT + subq_17.buys AS visit_buy_conversions +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_5.visits) AS visits + , COALESCE(MAX(subq_16.buys), 0) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_4.visits) AS visits + FROM ( + -- Pass Only Elements: ['visits',] + SELECT + subq_3.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_2.visit__referrer_id + , subq_2.visits + FROM ( + -- Pass Only Elements: ['visits', 'visit__referrer_id'] + SELECT + subq_1.visit__referrer_id + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_0 + ) subq_1 + ) subq_2 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_3 + ) subq_4 + ) subq_5 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_15.buys) AS buys + FROM ( + -- Pass Only Elements: ['buys',] + SELECT + subq_14.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_13.ds__day + , subq_13.user + , subq_13.visit__referrer_id + , subq_13.buys + , subq_13.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_9.visits) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_9.visit__referrer_id) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visit__referrer_id + , FIRST_VALUE(subq_9.ds__day) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_9.user) OVER ( + PARTITION BY + subq_12.user + , subq_12.ds__day + , subq_12.mf_internal_uuid + ORDER BY subq_9.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_12.mf_internal_uuid AS mf_internal_uuid + , subq_12.buys AS buys + FROM ( + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] + SELECT + subq_8.ds__day + , subq_8.user + , subq_8.visit__referrer_id + , subq_8.visits + FROM ( + -- Constrain Output with WHERE + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_8 + ) subq_9 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.metric_time__day + , subq_11.metric_time__week + , subq_11.metric_time__month + , subq_11.metric_time__quarter + , subq_11.metric_time__year + , subq_11.metric_time__extract_year + , subq_11.metric_time__extract_quarter + , subq_11.metric_time__extract_month + , subq_11.metric_time__extract_day + , subq_11.metric_time__extract_dow + , subq_11.metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers + , uuid() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.ds__day AS metric_time__day + , subq_10.ds__week AS metric_time__week + , subq_10.ds__month AS metric_time__month + , subq_10.ds__quarter AS metric_time__quarter + , subq_10.ds__year AS metric_time__year + , subq_10.ds__extract_year AS metric_time__extract_year + , subq_10.ds__extract_quarter AS metric_time__extract_quarter + , subq_10.ds__extract_month AS metric_time__extract_month + , subq_10.ds__extract_day AS metric_time__extract_day + , subq_10.ds__extract_dow AS metric_time__extract_dow + , subq_10.ds__extract_doy AS metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_28000.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM buys_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_28000.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM buys_source_src_28000.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS buy__ds__extract_doy + , buys_source_src_28000.user_id AS user + , buys_source_src_28000.session_id + , buys_source_src_28000.user_id AS buy__user + , buys_source_src_28000.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_10 + ) subq_11 + ) subq_12 + ON + ( + subq_9.user = subq_12.user + ) AND ( + ( + subq_9.ds__day <= subq_12.ds__day + ) AND ( + subq_9.ds__day > DATE_ADD('day', -7, subq_12.ds__day) + ) + ) + ) subq_13 + ) subq_14 + ) subq_15 + ) subq_16 +) subq_17 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_filter_not_in_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_filter_not_in_group_by__plan0_optimized.sql new file mode 100644 index 0000000000..da06972689 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_filter_not_in_group_by__plan0_optimized.sql @@ -0,0 +1,107 @@ +-- Combine Aggregated Outputs +-- Compute Metrics via Expressions +SELECT + COALESCE(MAX(subq_34.buys), 0) AS visit_buy_conversions +FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits',] + -- Aggregate Measures + SELECT + 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_20 + WHERE visit__referrer_id = 'ref_id_01' +) subq_23 +CROSS JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: ['buys',] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_27.visits) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_27.visit__referrer_id) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visit__referrer_id + , FIRST_VALUE(subq_27.ds__day) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_27.user) OVER ( + PARTITION BY + subq_30.user + , subq_30.ds__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_30.mf_internal_uuid AS mf_internal_uuid + , subq_30.buys AS buys + FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] + SELECT + ds__day + , subq_25.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_25 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_27 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , uuid() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_30 + ON + ( + subq_27.user = subq_30.user + ) AND ( + ( + subq_27.ds__day <= subq_30.ds__day + ) AND ( + subq_27.ds__day > DATE_ADD('day', -7, subq_30.ds__day) + ) + ) + ) subq_31 +) subq_34 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_time_constraint__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_time_constraint__plan0.sql index aad475508d..37e404e044 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_time_constraint__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_time_constraint__plan0.sql @@ -1,13 +1,13 @@ -- Compute Metrics via Expressions SELECT - subq_17.visit__referrer_id - , CAST(subq_17.buys AS DOUBLE) / CAST(NULLIF(subq_17.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate + subq_18.visit__referrer_id + , CAST(subq_18.buys AS DOUBLE) / CAST(NULLIF(subq_18.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) AS visit__referrer_id , MAX(subq_5.visits) AS visits - , MAX(subq_16.buys) AS buys + , MAX(subq_17.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -158,277 +158,323 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_15.visit__referrer_id - , SUM(subq_15.buys) AS buys + subq_16.visit__referrer_id + , SUM(subq_16.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'visit__referrer_id'] SELECT - subq_14.visit__referrer_id - , subq_14.buys + subq_15.visit__referrer_id + , subq_15.buys FROM ( -- Find conversions for user within the range of INF SELECT - subq_13.ds__day - , subq_13.user - , subq_13.visit__referrer_id - , subq_13.buys - , subq_13.visits + subq_14.ds__day + , subq_14.user + , subq_14.visit__referrer_id + , subq_14.buys + , subq_14.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_9.visits) OVER ( + FIRST_VALUE(subq_10.visits) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_9.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_10.visit__referrer_id) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_9.ds__day) OVER ( + , FIRST_VALUE(subq_10.ds__day) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_9.user) OVER ( + , FIRST_VALUE(subq_10.user) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_12.mf_internal_uuid AS mf_internal_uuid - , subq_12.buys AS buys + , subq_13.mf_internal_uuid AS mf_internal_uuid + , subq_13.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] SELECT - subq_8.ds__day - , subq_8.user - , subq_8.visit__referrer_id - , subq_8.visits + subq_9.ds__day + , subq_9.user + , subq_9.visit__referrer_id + , subq_9.visits FROM ( - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE SELECT - subq_7.ds__day - , subq_7.ds__week - , subq_7.ds__month - , subq_7.ds__quarter - , subq_7.ds__year - , subq_7.ds__extract_year - , subq_7.ds__extract_quarter - , subq_7.ds__extract_month - , subq_7.ds__extract_day - , subq_7.ds__extract_dow - , subq_7.ds__extract_doy - , subq_7.visit__ds__day - , subq_7.visit__ds__week - , subq_7.visit__ds__month - , subq_7.visit__ds__quarter - , subq_7.visit__ds__year - , subq_7.visit__ds__extract_year - , subq_7.visit__ds__extract_quarter - , subq_7.visit__ds__extract_month - , subq_7.visit__ds__extract_day - , subq_7.visit__ds__extract_dow - , subq_7.visit__ds__extract_doy - , subq_7.metric_time__day - , subq_7.metric_time__week - , subq_7.metric_time__month - , subq_7.metric_time__quarter - , subq_7.metric_time__year - , subq_7.metric_time__extract_year - , subq_7.metric_time__extract_quarter - , subq_7.metric_time__extract_month - , subq_7.metric_time__extract_day - , subq_7.metric_time__extract_dow - , subq_7.metric_time__extract_doy - , subq_7.user - , subq_7.session - , subq_7.visit__user - , subq_7.visit__session - , subq_7.referrer_id - , subq_7.visit__referrer_id - , subq_7.visits - , subq_7.visitors + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.visit__ds__day + , subq_8.visit__ds__week + , subq_8.visit__ds__month + , subq_8.visit__ds__quarter + , subq_8.visit__ds__year + , subq_8.visit__ds__extract_year + , subq_8.visit__ds__extract_quarter + , subq_8.visit__ds__extract_month + , subq_8.visit__ds__extract_day + , subq_8.visit__ds__extract_dow + , subq_8.visit__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session + , subq_8.visit__user + , subq_8.visit__session + , subq_8.referrer_id + , subq_8.visit__referrer_id + , subq_8.visits + , subq_8.visitors FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] SELECT - subq_6.ds__day - , subq_6.ds__week - , subq_6.ds__month - , subq_6.ds__quarter - , subq_6.ds__year - , subq_6.ds__extract_year - , subq_6.ds__extract_quarter - , subq_6.ds__extract_month - , subq_6.ds__extract_day - , subq_6.ds__extract_dow - , subq_6.ds__extract_doy - , subq_6.visit__ds__day - , subq_6.visit__ds__week - , subq_6.visit__ds__month - , subq_6.visit__ds__quarter - , subq_6.visit__ds__year - , subq_6.visit__ds__extract_year - , subq_6.visit__ds__extract_quarter - , subq_6.visit__ds__extract_month - , subq_6.visit__ds__extract_day - , subq_6.visit__ds__extract_dow - , subq_6.visit__ds__extract_doy - , subq_6.ds__day AS metric_time__day - , subq_6.ds__week AS metric_time__week - , subq_6.ds__month AS metric_time__month - , subq_6.ds__quarter AS metric_time__quarter - , subq_6.ds__year AS metric_time__year - , subq_6.ds__extract_year AS metric_time__extract_year - , subq_6.ds__extract_quarter AS metric_time__extract_quarter - , subq_6.ds__extract_month AS metric_time__extract_month - , subq_6.ds__extract_day AS metric_time__extract_day - , subq_6.ds__extract_dow AS metric_time__extract_dow - , subq_6.ds__extract_doy AS metric_time__extract_doy - , subq_6.user - , subq_6.session - , subq_6.visit__user - , subq_6.visit__session - , subq_6.referrer_id - , subq_6.visit__referrer_id - , subq_6.visits - , subq_6.visitors + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_6 - ) subq_7 - WHERE subq_7.metric_time__day BETWEEN timestamp '2020-01-01' AND timestamp '2020-01-02' - ) subq_8 - ) subq_9 + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE subq_7.metric_time__day BETWEEN timestamp '2020-01-01' AND timestamp '2020-01-02' + ) subq_8 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_9 + ) subq_10 INNER JOIN ( -- Add column with generated UUID SELECT - subq_11.ds__day - , subq_11.ds__week - , subq_11.ds__month - , subq_11.ds__quarter - , subq_11.ds__year - , subq_11.ds__extract_year - , subq_11.ds__extract_quarter - , subq_11.ds__extract_month - , subq_11.ds__extract_day - , subq_11.ds__extract_dow - , subq_11.ds__extract_doy - , subq_11.buy__ds__day - , subq_11.buy__ds__week - , subq_11.buy__ds__month - , subq_11.buy__ds__quarter - , subq_11.buy__ds__year - , subq_11.buy__ds__extract_year - , subq_11.buy__ds__extract_quarter - , subq_11.buy__ds__extract_month - , subq_11.buy__ds__extract_day - , subq_11.buy__ds__extract_dow - , subq_11.buy__ds__extract_doy - , subq_11.metric_time__day - , subq_11.metric_time__week - , subq_11.metric_time__month - , subq_11.metric_time__quarter - , subq_11.metric_time__year - , subq_11.metric_time__extract_year - , subq_11.metric_time__extract_quarter - , subq_11.metric_time__extract_month - , subq_11.metric_time__extract_day - , subq_11.metric_time__extract_dow - , subq_11.metric_time__extract_doy - , subq_11.user - , subq_11.session_id - , subq_11.buy__user - , subq_11.buy__session_id - , subq_11.buys - , subq_11.buyers + subq_12.ds__day + , subq_12.ds__week + , subq_12.ds__month + , subq_12.ds__quarter + , subq_12.ds__year + , subq_12.ds__extract_year + , subq_12.ds__extract_quarter + , subq_12.ds__extract_month + , subq_12.ds__extract_day + , subq_12.ds__extract_dow + , subq_12.ds__extract_doy + , subq_12.buy__ds__day + , subq_12.buy__ds__week + , subq_12.buy__ds__month + , subq_12.buy__ds__quarter + , subq_12.buy__ds__year + , subq_12.buy__ds__extract_year + , subq_12.buy__ds__extract_quarter + , subq_12.buy__ds__extract_month + , subq_12.buy__ds__extract_day + , subq_12.buy__ds__extract_dow + , subq_12.buy__ds__extract_doy + , subq_12.metric_time__day + , subq_12.metric_time__week + , subq_12.metric_time__month + , subq_12.metric_time__quarter + , subq_12.metric_time__year + , subq_12.metric_time__extract_year + , subq_12.metric_time__extract_quarter + , subq_12.metric_time__extract_month + , subq_12.metric_time__extract_day + , subq_12.metric_time__extract_dow + , subq_12.metric_time__extract_doy + , subq_12.user + , subq_12.session_id + , subq_12.buy__user + , subq_12.buy__session_id + , subq_12.buys + , subq_12.buyers , uuid() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_10.ds__day - , subq_10.ds__week - , subq_10.ds__month - , subq_10.ds__quarter - , subq_10.ds__year - , subq_10.ds__extract_year - , subq_10.ds__extract_quarter - , subq_10.ds__extract_month - , subq_10.ds__extract_day - , subq_10.ds__extract_dow - , subq_10.ds__extract_doy - , subq_10.buy__ds__day - , subq_10.buy__ds__week - , subq_10.buy__ds__month - , subq_10.buy__ds__quarter - , subq_10.buy__ds__year - , subq_10.buy__ds__extract_year - , subq_10.buy__ds__extract_quarter - , subq_10.buy__ds__extract_month - , subq_10.buy__ds__extract_day - , subq_10.buy__ds__extract_dow - , subq_10.buy__ds__extract_doy - , subq_10.ds__day AS metric_time__day - , subq_10.ds__week AS metric_time__week - , subq_10.ds__month AS metric_time__month - , subq_10.ds__quarter AS metric_time__quarter - , subq_10.ds__year AS metric_time__year - , subq_10.ds__extract_year AS metric_time__extract_year - , subq_10.ds__extract_quarter AS metric_time__extract_quarter - , subq_10.ds__extract_month AS metric_time__extract_month - , subq_10.ds__extract_day AS metric_time__extract_day - , subq_10.ds__extract_dow AS metric_time__extract_dow - , subq_10.ds__extract_doy AS metric_time__extract_doy - , subq_10.user - , subq_10.session_id - , subq_10.buy__user - , subq_10.buy__session_id - , subq_10.buys - , subq_10.buyers + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.ds__day AS metric_time__day + , subq_11.ds__week AS metric_time__week + , subq_11.ds__month AS metric_time__month + , subq_11.ds__quarter AS metric_time__quarter + , subq_11.ds__year AS metric_time__year + , subq_11.ds__extract_year AS metric_time__extract_year + , subq_11.ds__extract_quarter AS metric_time__extract_quarter + , subq_11.ds__extract_month AS metric_time__extract_month + , subq_11.ds__extract_day AS metric_time__extract_day + , subq_11.ds__extract_dow AS metric_time__extract_dow + , subq_11.ds__extract_doy AS metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -461,23 +507,23 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_10 - ) subq_11 - ) subq_12 + ) subq_11 + ) subq_12 + ) subq_13 ON ( - subq_9.user = subq_12.user + subq_10.user = subq_13.user ) AND ( - (subq_9.ds__day <= subq_12.ds__day) + (subq_10.ds__day <= subq_13.ds__day) ) - ) subq_13 - ) subq_14 - ) subq_15 + ) subq_14 + ) subq_15 + ) subq_16 GROUP BY - subq_15.visit__referrer_id - ) subq_16 + subq_16.visit__referrer_id + ) subq_17 ON - subq_5.visit__referrer_id = subq_16.visit__referrer_id + subq_5.visit__referrer_id = subq_17.visit__referrer_id GROUP BY - COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) -) subq_17 + COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) +) subq_18 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_time_constraint__plan0_optimized.sql index 156f22dc55..d77212989e 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_time_constraint__plan0_optimized.sql @@ -5,9 +5,9 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) AS visit__referrer_id - , MAX(subq_23.visits) AS visits - , MAX(subq_34.buys) AS buys + COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) AS visit__referrer_id + , MAX(subq_24.visits) AS visits + , MAX(subq_36.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -24,11 +24,11 @@ FROM ( , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 WHERE DATE_TRUNC('day', ds) BETWEEN timestamp '2020-01-01' AND timestamp '2020-01-02' - ) subq_21 + ) subq_22 WHERE visit__referrer_id = 'ref_id_01' GROUP BY visit__referrer_id - ) subq_23 + ) subq_24 FULL OUTER JOIN ( -- Find conversions for user within the range of INF -- Pass Only Elements: ['buys', 'visit__referrer_id'] @@ -39,53 +39,62 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_27.visits) OVER ( + FIRST_VALUE(subq_29.visits) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_27.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_29.visit__referrer_id) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_27.ds__day) OVER ( + , FIRST_VALUE(subq_29.ds__day) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_27.user) OVER ( + , FIRST_VALUE(subq_29.user) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_30.mf_internal_uuid AS mf_internal_uuid - , subq_30.buys AS buys + , subq_32.mf_internal_uuid AS mf_internal_uuid + , subq_32.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - WHERE DATE_TRUNC('day', ds) BETWEEN timestamp '2020-01-01' AND timestamp '2020-01-02' - ) subq_27 + ds__day + , subq_27.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + WHERE DATE_TRUNC('day', ds) BETWEEN timestamp '2020-01-01' AND timestamp '2020-01-02' + ) subq_27 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_29 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -96,19 +105,19 @@ FROM ( , 1 AS buys , uuid() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_30 + ) subq_32 ON ( - subq_27.user = subq_30.user + subq_29.user = subq_32.user ) AND ( - (subq_27.ds__day <= subq_30.ds__day) + (subq_29.ds__day <= subq_32.ds__day) ) - ) subq_31 + ) subq_33 GROUP BY visit__referrer_id - ) subq_34 + ) subq_36 ON - subq_23.visit__referrer_id = subq_34.visit__referrer_id + subq_24.visit__referrer_id = subq_36.visit__referrer_id GROUP BY - COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) -) subq_35 + COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) +) subq_37 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_window__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_window__plan0.sql index e11187b2a3..cc7a53c7c4 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_window__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_window__plan0.sql @@ -1,13 +1,13 @@ -- Compute Metrics via Expressions SELECT - subq_15.metric_time__day - , CAST(subq_15.buys AS DOUBLE) / CAST(NULLIF(subq_15.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days + subq_16.metric_time__day + , CAST(subq_16.buys AS DOUBLE) / CAST(NULLIF(subq_16.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) AS metric_time__day + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) AS metric_time__day , MAX(subq_4.visits) AS visits - , MAX(subq_14.buys) AS buys + , MAX(subq_15.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -112,231 +112,277 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_13.metric_time__day - , SUM(subq_13.buys) AS buys + subq_14.metric_time__day + , SUM(subq_14.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'metric_time__day'] SELECT - subq_12.metric_time__day - , subq_12.buys + subq_13.metric_time__day + , subq_13.buys FROM ( -- Find conversions for user within the range of 7 day SELECT - subq_11.ds__day - , subq_11.metric_time__day - , subq_11.user - , subq_11.buys - , subq_11.visits + subq_12.ds__day + , subq_12.metric_time__day + , subq_12.user + , subq_12.buys + , subq_12.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_7.visits) OVER ( + FIRST_VALUE(subq_8.visits) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_7.ds__day) OVER ( + , FIRST_VALUE(subq_8.ds__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_7.metric_time__day) OVER ( + , FIRST_VALUE(subq_8.metric_time__day) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_7.user) OVER ( + , FIRST_VALUE(subq_8.user) OVER ( PARTITION BY - subq_10.user - , subq_10.ds__day - , subq_10.mf_internal_uuid - ORDER BY subq_7.ds__day DESC + subq_11.user + , subq_11.ds__day + , subq_11.mf_internal_uuid + ORDER BY subq_8.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_10.mf_internal_uuid AS mf_internal_uuid - , subq_10.buys AS buys + , subq_11.mf_internal_uuid AS mf_internal_uuid + , subq_11.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_6.ds__day - , subq_6.metric_time__day - , subq_6.user - , subq_6.visits + subq_7.ds__day + , subq_7.metric_time__day + , subq_7.user + , subq_7.visits FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE SELECT - subq_5.ds__day - , subq_5.ds__week - , subq_5.ds__month - , subq_5.ds__quarter - , subq_5.ds__year - , subq_5.ds__extract_year - , subq_5.ds__extract_quarter - , subq_5.ds__extract_month - , subq_5.ds__extract_day - , subq_5.ds__extract_dow - , subq_5.ds__extract_doy - , subq_5.visit__ds__day - , subq_5.visit__ds__week - , subq_5.visit__ds__month - , subq_5.visit__ds__quarter - , subq_5.visit__ds__year - , subq_5.visit__ds__extract_year - , subq_5.visit__ds__extract_quarter - , subq_5.visit__ds__extract_month - , subq_5.visit__ds__extract_day - , subq_5.visit__ds__extract_dow - , subq_5.visit__ds__extract_doy - , subq_5.ds__day AS metric_time__day - , subq_5.ds__week AS metric_time__week - , subq_5.ds__month AS metric_time__month - , subq_5.ds__quarter AS metric_time__quarter - , subq_5.ds__year AS metric_time__year - , subq_5.ds__extract_year AS metric_time__extract_year - , subq_5.ds__extract_quarter AS metric_time__extract_quarter - , subq_5.ds__extract_month AS metric_time__extract_month - , subq_5.ds__extract_day AS metric_time__extract_day - , subq_5.ds__extract_dow AS metric_time__extract_dow - , subq_5.ds__extract_doy AS metric_time__extract_doy - , subq_5.user - , subq_5.session - , subq_5.visit__user - , subq_5.visit__session - , subq_5.referrer_id - , subq_5.visit__referrer_id - , subq_5.visits - , subq_5.visitors + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.metric_time__day + , subq_6.metric_time__week + , subq_6.metric_time__month + , subq_6.metric_time__quarter + , subq_6.metric_time__year + , subq_6.metric_time__extract_year + , subq_6.metric_time__extract_quarter + , subq_6.metric_time__extract_month + , subq_6.metric_time__extract_day + , subq_6.metric_time__extract_dow + , subq_6.metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_5 - ) subq_6 - ) subq_7 + subq_5.ds__day + , subq_5.ds__week + , subq_5.ds__month + , subq_5.ds__quarter + , subq_5.ds__year + , subq_5.ds__extract_year + , subq_5.ds__extract_quarter + , subq_5.ds__extract_month + , subq_5.ds__extract_day + , subq_5.ds__extract_dow + , subq_5.ds__extract_doy + , subq_5.visit__ds__day + , subq_5.visit__ds__week + , subq_5.visit__ds__month + , subq_5.visit__ds__quarter + , subq_5.visit__ds__year + , subq_5.visit__ds__extract_year + , subq_5.visit__ds__extract_quarter + , subq_5.visit__ds__extract_month + , subq_5.visit__ds__extract_day + , subq_5.visit__ds__extract_dow + , subq_5.visit__ds__extract_doy + , subq_5.ds__day AS metric_time__day + , subq_5.ds__week AS metric_time__week + , subq_5.ds__month AS metric_time__month + , subq_5.ds__quarter AS metric_time__quarter + , subq_5.ds__year AS metric_time__year + , subq_5.ds__extract_year AS metric_time__extract_year + , subq_5.ds__extract_quarter AS metric_time__extract_quarter + , subq_5.ds__extract_month AS metric_time__extract_month + , subq_5.ds__extract_day AS metric_time__extract_day + , subq_5.ds__extract_dow AS metric_time__extract_dow + , subq_5.ds__extract_doy AS metric_time__extract_doy + , subq_5.user + , subq_5.session + , subq_5.visit__user + , subq_5.visit__session + , subq_5.referrer_id + , subq_5.visit__referrer_id + , subq_5.visits + , subq_5.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_5 + ) subq_6 + WHERE metric_time__day = '2020-01-01' + ) subq_7 + ) subq_8 INNER JOIN ( -- Add column with generated UUID SELECT - subq_9.ds__day - , subq_9.ds__week - , subq_9.ds__month - , subq_9.ds__quarter - , subq_9.ds__year - , subq_9.ds__extract_year - , subq_9.ds__extract_quarter - , subq_9.ds__extract_month - , subq_9.ds__extract_day - , subq_9.ds__extract_dow - , subq_9.ds__extract_doy - , subq_9.buy__ds__day - , subq_9.buy__ds__week - , subq_9.buy__ds__month - , subq_9.buy__ds__quarter - , subq_9.buy__ds__year - , subq_9.buy__ds__extract_year - , subq_9.buy__ds__extract_quarter - , subq_9.buy__ds__extract_month - , subq_9.buy__ds__extract_day - , subq_9.buy__ds__extract_dow - , subq_9.buy__ds__extract_doy - , subq_9.metric_time__day - , subq_9.metric_time__week - , subq_9.metric_time__month - , subq_9.metric_time__quarter - , subq_9.metric_time__year - , subq_9.metric_time__extract_year - , subq_9.metric_time__extract_quarter - , subq_9.metric_time__extract_month - , subq_9.metric_time__extract_day - , subq_9.metric_time__extract_dow - , subq_9.metric_time__extract_doy - , subq_9.user - , subq_9.session_id - , subq_9.buy__user - , subq_9.buy__session_id - , subq_9.buys - , subq_9.buyers + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.buy__ds__day + , subq_10.buy__ds__week + , subq_10.buy__ds__month + , subq_10.buy__ds__quarter + , subq_10.buy__ds__year + , subq_10.buy__ds__extract_year + , subq_10.buy__ds__extract_quarter + , subq_10.buy__ds__extract_month + , subq_10.buy__ds__extract_day + , subq_10.buy__ds__extract_dow + , subq_10.buy__ds__extract_doy + , subq_10.metric_time__day + , subq_10.metric_time__week + , subq_10.metric_time__month + , subq_10.metric_time__quarter + , subq_10.metric_time__year + , subq_10.metric_time__extract_year + , subq_10.metric_time__extract_quarter + , subq_10.metric_time__extract_month + , subq_10.metric_time__extract_day + , subq_10.metric_time__extract_dow + , subq_10.metric_time__extract_doy + , subq_10.user + , subq_10.session_id + , subq_10.buy__user + , subq_10.buy__session_id + , subq_10.buys + , subq_10.buyers , uuid() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_8.ds__day - , subq_8.ds__week - , subq_8.ds__month - , subq_8.ds__quarter - , subq_8.ds__year - , subq_8.ds__extract_year - , subq_8.ds__extract_quarter - , subq_8.ds__extract_month - , subq_8.ds__extract_day - , subq_8.ds__extract_dow - , subq_8.ds__extract_doy - , subq_8.buy__ds__day - , subq_8.buy__ds__week - , subq_8.buy__ds__month - , subq_8.buy__ds__quarter - , subq_8.buy__ds__year - , subq_8.buy__ds__extract_year - , subq_8.buy__ds__extract_quarter - , subq_8.buy__ds__extract_month - , subq_8.buy__ds__extract_day - , subq_8.buy__ds__extract_dow - , subq_8.buy__ds__extract_doy - , subq_8.ds__day AS metric_time__day - , subq_8.ds__week AS metric_time__week - , subq_8.ds__month AS metric_time__month - , subq_8.ds__quarter AS metric_time__quarter - , subq_8.ds__year AS metric_time__year - , subq_8.ds__extract_year AS metric_time__extract_year - , subq_8.ds__extract_quarter AS metric_time__extract_quarter - , subq_8.ds__extract_month AS metric_time__extract_month - , subq_8.ds__extract_day AS metric_time__extract_day - , subq_8.ds__extract_dow AS metric_time__extract_dow - , subq_8.ds__extract_doy AS metric_time__extract_doy - , subq_8.user - , subq_8.session_id - , subq_8.buy__user - , subq_8.buy__session_id - , subq_8.buys - , subq_8.buyers + subq_9.ds__day + , subq_9.ds__week + , subq_9.ds__month + , subq_9.ds__quarter + , subq_9.ds__year + , subq_9.ds__extract_year + , subq_9.ds__extract_quarter + , subq_9.ds__extract_month + , subq_9.ds__extract_day + , subq_9.ds__extract_dow + , subq_9.ds__extract_doy + , subq_9.buy__ds__day + , subq_9.buy__ds__week + , subq_9.buy__ds__month + , subq_9.buy__ds__quarter + , subq_9.buy__ds__year + , subq_9.buy__ds__extract_year + , subq_9.buy__ds__extract_quarter + , subq_9.buy__ds__extract_month + , subq_9.buy__ds__extract_day + , subq_9.buy__ds__extract_dow + , subq_9.buy__ds__extract_doy + , subq_9.ds__day AS metric_time__day + , subq_9.ds__week AS metric_time__week + , subq_9.ds__month AS metric_time__month + , subq_9.ds__quarter AS metric_time__quarter + , subq_9.ds__year AS metric_time__year + , subq_9.ds__extract_year AS metric_time__extract_year + , subq_9.ds__extract_quarter AS metric_time__extract_quarter + , subq_9.ds__extract_month AS metric_time__extract_month + , subq_9.ds__extract_day AS metric_time__extract_day + , subq_9.ds__extract_dow AS metric_time__extract_dow + , subq_9.ds__extract_doy AS metric_time__extract_doy + , subq_9.user + , subq_9.session_id + , subq_9.buy__user + , subq_9.buy__session_id + , subq_9.buys + , subq_9.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -369,27 +415,27 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_8 - ) subq_9 - ) subq_10 + ) subq_9 + ) subq_10 + ) subq_11 ON ( - subq_7.user = subq_10.user + subq_8.user = subq_11.user ) AND ( ( - subq_7.ds__day <= subq_10.ds__day + subq_8.ds__day <= subq_11.ds__day ) AND ( - subq_7.ds__day > DATE_ADD('day', -7, subq_10.ds__day) + subq_8.ds__day > DATE_ADD('day', -7, subq_11.ds__day) ) ) - ) subq_11 - ) subq_12 - ) subq_13 + ) subq_12 + ) subq_13 + ) subq_14 GROUP BY - subq_13.metric_time__day - ) subq_14 + subq_14.metric_time__day + ) subq_15 ON - subq_4.metric_time__day = subq_14.metric_time__day + subq_4.metric_time__day = subq_15.metric_time__day GROUP BY - COALESCE(subq_4.metric_time__day, subq_14.metric_time__day) -) subq_15 + COALESCE(subq_4.metric_time__day, subq_15.metric_time__day) +) subq_16 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_window__plan0_optimized.sql index 818bd3b5df..220f7f6ffb 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_window__plan0_optimized.sql @@ -5,9 +5,9 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_20.visits) AS visits - , MAX(subq_30.buys) AS buys + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , MAX(subq_21.visits) AS visits + , MAX(subq_32.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -22,11 +22,11 @@ FROM ( DATE_TRUNC('day', ds) AS metric_time__day , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_18 + ) subq_19 WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__day - ) subq_20 + ) subq_21 FULL OUTER JOIN ( -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'metric_time__day'] @@ -37,51 +37,60 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_23.visits) OVER ( + FIRST_VALUE(subq_25.visits) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_23.ds__day) OVER ( + , FIRST_VALUE(subq_25.ds__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_23.metric_time__day) OVER ( + , FIRST_VALUE(subq_25.metric_time__day) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_23.user) OVER ( + , FIRST_VALUE(subq_25.user) OVER ( PARTITION BY - subq_26.user - , subq_26.ds__day - , subq_26.mf_internal_uuid - ORDER BY subq_23.ds__day DESC + subq_28.user + , subq_28.ds__day + , subq_28.mf_internal_uuid + ORDER BY subq_25.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_26.mf_internal_uuid AS mf_internal_uuid - , subq_26.buys AS buys + , subq_28.mf_internal_uuid AS mf_internal_uuid + , subq_28.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'ds__day', 'metric_time__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + ds__day + , metric_time__day + , subq_23.user + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_23 + WHERE metric_time__day = '2020-01-01' + ) subq_25 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -92,23 +101,23 @@ FROM ( , 1 AS buys , uuid() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_26 + ) subq_28 ON ( - subq_23.user = subq_26.user + subq_25.user = subq_28.user ) AND ( ( - subq_23.ds__day <= subq_26.ds__day + subq_25.ds__day <= subq_28.ds__day ) AND ( - subq_23.ds__day > DATE_ADD('day', -7, subq_26.ds__day) + subq_25.ds__day > DATE_ADD('day', -7, subq_28.ds__day) ) ) - ) subq_27 + ) subq_29 GROUP BY metric_time__day - ) subq_30 + ) subq_32 ON - subq_20.metric_time__day = subq_30.metric_time__day + subq_21.metric_time__day = subq_32.metric_time__day GROUP BY - COALESCE(subq_20.metric_time__day, subq_30.metric_time__day) -) subq_31 + COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) +) subq_33 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_window_and_time_constraint__plan0.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_window_and_time_constraint__plan0.sql index fd4d76d852..61bc9c931f 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_window_and_time_constraint__plan0.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_window_and_time_constraint__plan0.sql @@ -1,15 +1,15 @@ -- Compute Metrics via Expressions SELECT - subq_17.metric_time__day - , subq_17.visit__referrer_id - , CAST(subq_17.buys AS DOUBLE) / CAST(NULLIF(subq_17.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days + subq_18.metric_time__day + , subq_18.visit__referrer_id + , CAST(subq_18.buys AS DOUBLE) / CAST(NULLIF(subq_18.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_5.metric_time__day, subq_16.metric_time__day) AS metric_time__day - , COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_5.metric_time__day, subq_17.metric_time__day) AS metric_time__day + , COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) AS visit__referrer_id , MAX(subq_5.visits) AS visits - , MAX(subq_16.buys) AS buys + , MAX(subq_17.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -164,289 +164,335 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_15.metric_time__day - , subq_15.visit__referrer_id - , SUM(subq_15.buys) AS buys + subq_16.metric_time__day + , subq_16.visit__referrer_id + , SUM(subq_16.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] SELECT - subq_14.metric_time__day - , subq_14.visit__referrer_id - , subq_14.buys + subq_15.metric_time__day + , subq_15.visit__referrer_id + , subq_15.buys FROM ( -- Find conversions for user within the range of 7 day SELECT - subq_13.ds__day - , subq_13.metric_time__day - , subq_13.user - , subq_13.visit__referrer_id - , subq_13.buys - , subq_13.visits + subq_14.ds__day + , subq_14.metric_time__day + , subq_14.user + , subq_14.visit__referrer_id + , subq_14.buys + , subq_14.visits FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_9.visits) OVER ( + FIRST_VALUE(subq_10.visits) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_9.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_10.visit__referrer_id) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_9.ds__day) OVER ( + , FIRST_VALUE(subq_10.ds__day) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_9.metric_time__day) OVER ( + , FIRST_VALUE(subq_10.metric_time__day) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_9.user) OVER ( + , FIRST_VALUE(subq_10.user) OVER ( PARTITION BY - subq_12.user - , subq_12.ds__day - , subq_12.mf_internal_uuid - ORDER BY subq_9.ds__day DESC + subq_13.user + , subq_13.ds__day + , subq_13.mf_internal_uuid + ORDER BY subq_10.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_12.mf_internal_uuid AS mf_internal_uuid - , subq_12.buys AS buys + , subq_13.mf_internal_uuid AS mf_internal_uuid + , subq_13.buys AS buys FROM ( -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_8.ds__day - , subq_8.metric_time__day - , subq_8.user - , subq_8.visit__referrer_id - , subq_8.visits + subq_9.ds__day + , subq_9.metric_time__day + , subq_9.user + , subq_9.visit__referrer_id + , subq_9.visits FROM ( - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE SELECT - subq_7.ds__day - , subq_7.ds__week - , subq_7.ds__month - , subq_7.ds__quarter - , subq_7.ds__year - , subq_7.ds__extract_year - , subq_7.ds__extract_quarter - , subq_7.ds__extract_month - , subq_7.ds__extract_day - , subq_7.ds__extract_dow - , subq_7.ds__extract_doy - , subq_7.visit__ds__day - , subq_7.visit__ds__week - , subq_7.visit__ds__month - , subq_7.visit__ds__quarter - , subq_7.visit__ds__year - , subq_7.visit__ds__extract_year - , subq_7.visit__ds__extract_quarter - , subq_7.visit__ds__extract_month - , subq_7.visit__ds__extract_day - , subq_7.visit__ds__extract_dow - , subq_7.visit__ds__extract_doy - , subq_7.metric_time__day - , subq_7.metric_time__week - , subq_7.metric_time__month - , subq_7.metric_time__quarter - , subq_7.metric_time__year - , subq_7.metric_time__extract_year - , subq_7.metric_time__extract_quarter - , subq_7.metric_time__extract_month - , subq_7.metric_time__extract_day - , subq_7.metric_time__extract_dow - , subq_7.metric_time__extract_doy - , subq_7.user - , subq_7.session - , subq_7.visit__user - , subq_7.visit__session - , subq_7.referrer_id - , subq_7.visit__referrer_id - , subq_7.visits - , subq_7.visitors + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.visit__ds__day + , subq_8.visit__ds__week + , subq_8.visit__ds__month + , subq_8.visit__ds__quarter + , subq_8.visit__ds__year + , subq_8.visit__ds__extract_year + , subq_8.visit__ds__extract_quarter + , subq_8.visit__ds__extract_month + , subq_8.visit__ds__extract_day + , subq_8.visit__ds__extract_dow + , subq_8.visit__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session + , subq_8.visit__user + , subq_8.visit__session + , subq_8.referrer_id + , subq_8.visit__referrer_id + , subq_8.visits + , subq_8.visitors FROM ( - -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] SELECT - subq_6.ds__day - , subq_6.ds__week - , subq_6.ds__month - , subq_6.ds__quarter - , subq_6.ds__year - , subq_6.ds__extract_year - , subq_6.ds__extract_quarter - , subq_6.ds__extract_month - , subq_6.ds__extract_day - , subq_6.ds__extract_dow - , subq_6.ds__extract_doy - , subq_6.visit__ds__day - , subq_6.visit__ds__week - , subq_6.visit__ds__month - , subq_6.visit__ds__quarter - , subq_6.visit__ds__year - , subq_6.visit__ds__extract_year - , subq_6.visit__ds__extract_quarter - , subq_6.visit__ds__extract_month - , subq_6.visit__ds__extract_day - , subq_6.visit__ds__extract_dow - , subq_6.visit__ds__extract_doy - , subq_6.ds__day AS metric_time__day - , subq_6.ds__week AS metric_time__week - , subq_6.ds__month AS metric_time__month - , subq_6.ds__quarter AS metric_time__quarter - , subq_6.ds__year AS metric_time__year - , subq_6.ds__extract_year AS metric_time__extract_year - , subq_6.ds__extract_quarter AS metric_time__extract_quarter - , subq_6.ds__extract_month AS metric_time__extract_month - , subq_6.ds__extract_day AS metric_time__extract_day - , subq_6.ds__extract_dow AS metric_time__extract_dow - , subq_6.ds__extract_doy AS metric_time__extract_doy - , subq_6.user - , subq_6.session - , subq_6.visit__user - , subq_6.visit__session - , subq_6.referrer_id - , subq_6.visit__referrer_id - , subq_6.visits - , subq_6.visitors + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.visit__ds__day + , subq_7.visit__ds__week + , subq_7.visit__ds__month + , subq_7.visit__ds__quarter + , subq_7.visit__ds__year + , subq_7.visit__ds__extract_year + , subq_7.visit__ds__extract_quarter + , subq_7.visit__ds__extract_month + , subq_7.visit__ds__extract_day + , subq_7.visit__ds__extract_dow + , subq_7.visit__ds__extract_doy + , subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.metric_time__month + , subq_7.metric_time__quarter + , subq_7.metric_time__year + , subq_7.metric_time__extract_year + , subq_7.metric_time__extract_quarter + , subq_7.metric_time__extract_month + , subq_7.metric_time__extract_day + , subq_7.metric_time__extract_dow + , subq_7.metric_time__extract_doy + , subq_7.user + , subq_7.session + , subq_7.visit__user + , subq_7.visit__session + , subq_7.referrer_id + , subq_7.visit__referrer_id + , subq_7.visits + , subq_7.visitors FROM ( - -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_6 - ) subq_7 - WHERE subq_7.metric_time__day BETWEEN timestamp '2020-01-01' AND timestamp '2020-01-02' - ) subq_8 - ) subq_9 + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.visit__ds__day + , subq_6.visit__ds__week + , subq_6.visit__ds__month + , subq_6.visit__ds__quarter + , subq_6.visit__ds__year + , subq_6.visit__ds__extract_year + , subq_6.visit__ds__extract_quarter + , subq_6.visit__ds__extract_month + , subq_6.visit__ds__extract_day + , subq_6.visit__ds__extract_dow + , subq_6.visit__ds__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.user + , subq_6.session + , subq_6.visit__user + , subq_6.visit__session + , subq_6.referrer_id + , subq_6.visit__referrer_id + , subq_6.visits + , subq_6.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_6 + ) subq_7 + WHERE subq_7.metric_time__day BETWEEN timestamp '2020-01-01' AND timestamp '2020-01-02' + ) subq_8 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_9 + ) subq_10 INNER JOIN ( -- Add column with generated UUID SELECT - subq_11.ds__day - , subq_11.ds__week - , subq_11.ds__month - , subq_11.ds__quarter - , subq_11.ds__year - , subq_11.ds__extract_year - , subq_11.ds__extract_quarter - , subq_11.ds__extract_month - , subq_11.ds__extract_day - , subq_11.ds__extract_dow - , subq_11.ds__extract_doy - , subq_11.buy__ds__day - , subq_11.buy__ds__week - , subq_11.buy__ds__month - , subq_11.buy__ds__quarter - , subq_11.buy__ds__year - , subq_11.buy__ds__extract_year - , subq_11.buy__ds__extract_quarter - , subq_11.buy__ds__extract_month - , subq_11.buy__ds__extract_day - , subq_11.buy__ds__extract_dow - , subq_11.buy__ds__extract_doy - , subq_11.metric_time__day - , subq_11.metric_time__week - , subq_11.metric_time__month - , subq_11.metric_time__quarter - , subq_11.metric_time__year - , subq_11.metric_time__extract_year - , subq_11.metric_time__extract_quarter - , subq_11.metric_time__extract_month - , subq_11.metric_time__extract_day - , subq_11.metric_time__extract_dow - , subq_11.metric_time__extract_doy - , subq_11.user - , subq_11.session_id - , subq_11.buy__user - , subq_11.buy__session_id - , subq_11.buys - , subq_11.buyers + subq_12.ds__day + , subq_12.ds__week + , subq_12.ds__month + , subq_12.ds__quarter + , subq_12.ds__year + , subq_12.ds__extract_year + , subq_12.ds__extract_quarter + , subq_12.ds__extract_month + , subq_12.ds__extract_day + , subq_12.ds__extract_dow + , subq_12.ds__extract_doy + , subq_12.buy__ds__day + , subq_12.buy__ds__week + , subq_12.buy__ds__month + , subq_12.buy__ds__quarter + , subq_12.buy__ds__year + , subq_12.buy__ds__extract_year + , subq_12.buy__ds__extract_quarter + , subq_12.buy__ds__extract_month + , subq_12.buy__ds__extract_day + , subq_12.buy__ds__extract_dow + , subq_12.buy__ds__extract_doy + , subq_12.metric_time__day + , subq_12.metric_time__week + , subq_12.metric_time__month + , subq_12.metric_time__quarter + , subq_12.metric_time__year + , subq_12.metric_time__extract_year + , subq_12.metric_time__extract_quarter + , subq_12.metric_time__extract_month + , subq_12.metric_time__extract_day + , subq_12.metric_time__extract_dow + , subq_12.metric_time__extract_doy + , subq_12.user + , subq_12.session_id + , subq_12.buy__user + , subq_12.buy__session_id + , subq_12.buys + , subq_12.buyers , uuid() AS mf_internal_uuid FROM ( -- Metric Time Dimension 'ds' SELECT - subq_10.ds__day - , subq_10.ds__week - , subq_10.ds__month - , subq_10.ds__quarter - , subq_10.ds__year - , subq_10.ds__extract_year - , subq_10.ds__extract_quarter - , subq_10.ds__extract_month - , subq_10.ds__extract_day - , subq_10.ds__extract_dow - , subq_10.ds__extract_doy - , subq_10.buy__ds__day - , subq_10.buy__ds__week - , subq_10.buy__ds__month - , subq_10.buy__ds__quarter - , subq_10.buy__ds__year - , subq_10.buy__ds__extract_year - , subq_10.buy__ds__extract_quarter - , subq_10.buy__ds__extract_month - , subq_10.buy__ds__extract_day - , subq_10.buy__ds__extract_dow - , subq_10.buy__ds__extract_doy - , subq_10.ds__day AS metric_time__day - , subq_10.ds__week AS metric_time__week - , subq_10.ds__month AS metric_time__month - , subq_10.ds__quarter AS metric_time__quarter - , subq_10.ds__year AS metric_time__year - , subq_10.ds__extract_year AS metric_time__extract_year - , subq_10.ds__extract_quarter AS metric_time__extract_quarter - , subq_10.ds__extract_month AS metric_time__extract_month - , subq_10.ds__extract_day AS metric_time__extract_day - , subq_10.ds__extract_dow AS metric_time__extract_dow - , subq_10.ds__extract_doy AS metric_time__extract_doy - , subq_10.user - , subq_10.session_id - , subq_10.buy__user - , subq_10.buy__session_id - , subq_10.buys - , subq_10.buyers + subq_11.ds__day + , subq_11.ds__week + , subq_11.ds__month + , subq_11.ds__quarter + , subq_11.ds__year + , subq_11.ds__extract_year + , subq_11.ds__extract_quarter + , subq_11.ds__extract_month + , subq_11.ds__extract_day + , subq_11.ds__extract_dow + , subq_11.ds__extract_doy + , subq_11.buy__ds__day + , subq_11.buy__ds__week + , subq_11.buy__ds__month + , subq_11.buy__ds__quarter + , subq_11.buy__ds__year + , subq_11.buy__ds__extract_year + , subq_11.buy__ds__extract_quarter + , subq_11.buy__ds__extract_month + , subq_11.buy__ds__extract_day + , subq_11.buy__ds__extract_dow + , subq_11.buy__ds__extract_doy + , subq_11.ds__day AS metric_time__day + , subq_11.ds__week AS metric_time__week + , subq_11.ds__month AS metric_time__month + , subq_11.ds__quarter AS metric_time__quarter + , subq_11.ds__year AS metric_time__year + , subq_11.ds__extract_year AS metric_time__extract_year + , subq_11.ds__extract_quarter AS metric_time__extract_quarter + , subq_11.ds__extract_month AS metric_time__extract_month + , subq_11.ds__extract_day AS metric_time__extract_day + , subq_11.ds__extract_dow AS metric_time__extract_dow + , subq_11.ds__extract_doy AS metric_time__extract_doy + , subq_11.user + , subq_11.session_id + , subq_11.buy__user + , subq_11.buy__session_id + , subq_11.buys + , subq_11.buyers FROM ( -- Read Elements From Semantic Model 'buys_source' SELECT @@ -479,33 +525,33 @@ FROM ( , buys_source_src_28000.user_id AS buy__user , buys_source_src_28000.session_id AS buy__session_id FROM ***************************.fct_buys buys_source_src_28000 - ) subq_10 - ) subq_11 - ) subq_12 + ) subq_11 + ) subq_12 + ) subq_13 ON ( - subq_9.user = subq_12.user + subq_10.user = subq_13.user ) AND ( ( - subq_9.ds__day <= subq_12.ds__day + subq_10.ds__day <= subq_13.ds__day ) AND ( - subq_9.ds__day > DATE_ADD('day', -7, subq_12.ds__day) + subq_10.ds__day > DATE_ADD('day', -7, subq_13.ds__day) ) ) - ) subq_13 - ) subq_14 - ) subq_15 + ) subq_14 + ) subq_15 + ) subq_16 GROUP BY - subq_15.metric_time__day - , subq_15.visit__referrer_id - ) subq_16 + subq_16.metric_time__day + , subq_16.visit__referrer_id + ) subq_17 ON ( - subq_5.visit__referrer_id = subq_16.visit__referrer_id + subq_5.visit__referrer_id = subq_17.visit__referrer_id ) AND ( - subq_5.metric_time__day = subq_16.metric_time__day + subq_5.metric_time__day = subq_17.metric_time__day ) GROUP BY - COALESCE(subq_5.metric_time__day, subq_16.metric_time__day) - , COALESCE(subq_5.visit__referrer_id, subq_16.visit__referrer_id) -) subq_17 + COALESCE(subq_5.metric_time__day, subq_17.metric_time__day) + , COALESCE(subq_5.visit__referrer_id, subq_17.visit__referrer_id) +) subq_18 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql index 585b86b4a1..f1bd0ff3ff 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql @@ -6,10 +6,10 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_23.metric_time__day, subq_34.metric_time__day) AS metric_time__day - , COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) AS visit__referrer_id - , MAX(subq_23.visits) AS visits - , MAX(subq_34.buys) AS buys + COALESCE(subq_24.metric_time__day, subq_36.metric_time__day) AS metric_time__day + , COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) AS visit__referrer_id + , MAX(subq_24.visits) AS visits + , MAX(subq_36.buys) AS buys FROM ( -- Constrain Output with WHERE -- Aggregate Measures @@ -28,12 +28,12 @@ FROM ( , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 WHERE DATE_TRUNC('day', ds) BETWEEN timestamp '2020-01-01' AND timestamp '2020-01-02' - ) subq_21 + ) subq_22 WHERE visit__referrer_id = 'ref_id_01' GROUP BY metric_time__day , visit__referrer_id - ) subq_23 + ) subq_24 FULL OUTER JOIN ( -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day'] @@ -45,62 +45,72 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_27.visits) OVER ( + FIRST_VALUE(subq_29.visits) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_27.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_29.visit__referrer_id) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_27.ds__day) OVER ( + , FIRST_VALUE(subq_29.ds__day) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_27.metric_time__day) OVER ( + , FIRST_VALUE(subq_29.metric_time__day) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_27.user) OVER ( + , FIRST_VALUE(subq_29.user) OVER ( PARTITION BY - subq_30.user - , subq_30.ds__day - , subq_30.mf_internal_uuid - ORDER BY subq_27.ds__day DESC + subq_32.user + , subq_32.ds__day + , subq_32.mf_internal_uuid + ORDER BY subq_29.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_30.mf_internal_uuid AS mf_internal_uuid - , subq_30.buys AS buys + , subq_32.mf_internal_uuid AS mf_internal_uuid + , subq_32.buys AS buys FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - WHERE DATE_TRUNC('day', ds) BETWEEN timestamp '2020-01-01' AND timestamp '2020-01-02' - ) subq_27 + ds__day + , metric_time__day + , subq_27.user + , visit__referrer_id + , visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + WHERE DATE_TRUNC('day', ds) BETWEEN timestamp '2020-01-01' AND timestamp '2020-01-02' + ) subq_27 + WHERE visit__referrer_id = 'ref_id_01' + ) subq_29 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -111,29 +121,29 @@ FROM ( , 1 AS buys , uuid() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_30 + ) subq_32 ON ( - subq_27.user = subq_30.user + subq_29.user = subq_32.user ) AND ( ( - subq_27.ds__day <= subq_30.ds__day + subq_29.ds__day <= subq_32.ds__day ) AND ( - subq_27.ds__day > DATE_ADD('day', -7, subq_30.ds__day) + subq_29.ds__day > DATE_ADD('day', -7, subq_32.ds__day) ) ) - ) subq_31 + ) subq_33 GROUP BY metric_time__day , visit__referrer_id - ) subq_34 + ) subq_36 ON ( - subq_23.visit__referrer_id = subq_34.visit__referrer_id + subq_24.visit__referrer_id = subq_36.visit__referrer_id ) AND ( - subq_23.metric_time__day = subq_34.metric_time__day + subq_24.metric_time__day = subq_36.metric_time__day ) GROUP BY - COALESCE(subq_23.metric_time__day, subq_34.metric_time__day) - , COALESCE(subq_23.visit__referrer_id, subq_34.visit__referrer_id) -) subq_35 + COALESCE(subq_24.metric_time__day, subq_36.metric_time__day) + , COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) +) subq_37 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/BigQuery/test_conversion_metric__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/BigQuery/test_conversion_metric__query_output.txt new file mode 100644 index 0000000000..d59c3ac1da --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/BigQuery/test_conversion_metric__query_output.txt @@ -0,0 +1,10 @@ +metric_time__day visit_buy_conversion_rate +------------------- --------------------------- +2020-01-01T00:00:00 0.50 +2020-01-02T00:00:00 0.50 +2020-01-03T00:00:00 0.67 +2020-01-04T00:00:00 None +2020-01-06T00:00:00 None +2020-01-07T00:00:00 1.00 +2020-01-10T00:00:00 1.00 +2020-01-12T00:00:00 None diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/BigQuery/test_conversion_metric_with_categorical_filter__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/BigQuery/test_conversion_metric_with_categorical_filter__query_output.txt new file mode 100644 index 0000000000..e1e5bd822d --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/BigQuery/test_conversion_metric_with_categorical_filter__query_output.txt @@ -0,0 +1,5 @@ +metric_time__day visit__referrer_id visit_buy_conversion_rate +------------------- -------------------- --------------------------- +2020-01-01T00:00:00 fb_ad_1 2 +2020-01-02T00:00:00 fb_ad_1 1 +2020-01-03T00:00:00 fb_ad_1 1 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/BigQuery/test_conversion_metric_with_filter_not_in_group_by__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/BigQuery/test_conversion_metric_with_filter_not_in_group_by__query_output.txt new file mode 100644 index 0000000000..e3046748a3 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/BigQuery/test_conversion_metric_with_filter_not_in_group_by__query_output.txt @@ -0,0 +1,3 @@ + visit_buy_conversions +----------------------- + 3 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/BigQuery/test_conversion_metric_with_time_constraint__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/BigQuery/test_conversion_metric_with_time_constraint__query_output.txt new file mode 100644 index 0000000000..3fb6f042e8 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/BigQuery/test_conversion_metric_with_time_constraint__query_output.txt @@ -0,0 +1,3 @@ +visit__referrer_id visit_buy_conversion_rate +-------------------- --------------------------- +fb_ad_1 1.5 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/BigQuery/test_conversion_metric_with_window__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/BigQuery/test_conversion_metric_with_window__query_output.txt new file mode 100644 index 0000000000..294fbf9259 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/BigQuery/test_conversion_metric_with_window__query_output.txt @@ -0,0 +1,10 @@ +metric_time__day visit_buy_conversion_rate_7days +------------------- --------------------------------- +2020-01-01T00:00:00 0.50 +2020-01-02T00:00:00 0.50 +2020-01-03T00:00:00 0.67 +2020-01-04T00:00:00 None +2020-01-06T00:00:00 None +2020-01-07T00:00:00 1.00 +2020-01-10T00:00:00 1.00 +2020-01-12T00:00:00 None diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/BigQuery/test_conversion_metric_with_window_and_time_constraint__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/BigQuery/test_conversion_metric_with_window_and_time_constraint__query_output.txt new file mode 100644 index 0000000000..b4958e8622 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/BigQuery/test_conversion_metric_with_window_and_time_constraint__query_output.txt @@ -0,0 +1,4 @@ +metric_time__day visit__referrer_id visit_buy_conversion_rate_7days +------------------- -------------------- --------------------------------- +2020-01-01T00:00:00 fb_ad_1 2 +2020-01-02T00:00:00 fb_ad_1 1 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Databricks/test_conversion_metric__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Databricks/test_conversion_metric__query_output.txt new file mode 100644 index 0000000000..d59c3ac1da --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Databricks/test_conversion_metric__query_output.txt @@ -0,0 +1,10 @@ +metric_time__day visit_buy_conversion_rate +------------------- --------------------------- +2020-01-01T00:00:00 0.50 +2020-01-02T00:00:00 0.50 +2020-01-03T00:00:00 0.67 +2020-01-04T00:00:00 None +2020-01-06T00:00:00 None +2020-01-07T00:00:00 1.00 +2020-01-10T00:00:00 1.00 +2020-01-12T00:00:00 None diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Databricks/test_conversion_metric_with_categorical_filter__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Databricks/test_conversion_metric_with_categorical_filter__query_output.txt new file mode 100644 index 0000000000..e1e5bd822d --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Databricks/test_conversion_metric_with_categorical_filter__query_output.txt @@ -0,0 +1,5 @@ +metric_time__day visit__referrer_id visit_buy_conversion_rate +------------------- -------------------- --------------------------- +2020-01-01T00:00:00 fb_ad_1 2 +2020-01-02T00:00:00 fb_ad_1 1 +2020-01-03T00:00:00 fb_ad_1 1 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Databricks/test_conversion_metric_with_filter_not_in_group_by__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Databricks/test_conversion_metric_with_filter_not_in_group_by__query_output.txt new file mode 100644 index 0000000000..e3046748a3 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Databricks/test_conversion_metric_with_filter_not_in_group_by__query_output.txt @@ -0,0 +1,3 @@ + visit_buy_conversions +----------------------- + 3 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Databricks/test_conversion_metric_with_time_constraint__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Databricks/test_conversion_metric_with_time_constraint__query_output.txt new file mode 100644 index 0000000000..3fb6f042e8 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Databricks/test_conversion_metric_with_time_constraint__query_output.txt @@ -0,0 +1,3 @@ +visit__referrer_id visit_buy_conversion_rate +-------------------- --------------------------- +fb_ad_1 1.5 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Databricks/test_conversion_metric_with_window__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Databricks/test_conversion_metric_with_window__query_output.txt new file mode 100644 index 0000000000..294fbf9259 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Databricks/test_conversion_metric_with_window__query_output.txt @@ -0,0 +1,10 @@ +metric_time__day visit_buy_conversion_rate_7days +------------------- --------------------------------- +2020-01-01T00:00:00 0.50 +2020-01-02T00:00:00 0.50 +2020-01-03T00:00:00 0.67 +2020-01-04T00:00:00 None +2020-01-06T00:00:00 None +2020-01-07T00:00:00 1.00 +2020-01-10T00:00:00 1.00 +2020-01-12T00:00:00 None diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Databricks/test_conversion_metric_with_window_and_time_constraint__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Databricks/test_conversion_metric_with_window_and_time_constraint__query_output.txt new file mode 100644 index 0000000000..b4958e8622 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Databricks/test_conversion_metric_with_window_and_time_constraint__query_output.txt @@ -0,0 +1,4 @@ +metric_time__day visit__referrer_id visit_buy_conversion_rate_7days +------------------- -------------------- --------------------------------- +2020-01-01T00:00:00 fb_ad_1 2 +2020-01-02T00:00:00 fb_ad_1 1 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/DuckDB/test_conversion_metric__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/DuckDB/test_conversion_metric__query_output.txt new file mode 100644 index 0000000000..d59c3ac1da --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/DuckDB/test_conversion_metric__query_output.txt @@ -0,0 +1,10 @@ +metric_time__day visit_buy_conversion_rate +------------------- --------------------------- +2020-01-01T00:00:00 0.50 +2020-01-02T00:00:00 0.50 +2020-01-03T00:00:00 0.67 +2020-01-04T00:00:00 None +2020-01-06T00:00:00 None +2020-01-07T00:00:00 1.00 +2020-01-10T00:00:00 1.00 +2020-01-12T00:00:00 None diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/DuckDB/test_conversion_metric_with_categorical_filter__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/DuckDB/test_conversion_metric_with_categorical_filter__query_output.txt new file mode 100644 index 0000000000..e1e5bd822d --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/DuckDB/test_conversion_metric_with_categorical_filter__query_output.txt @@ -0,0 +1,5 @@ +metric_time__day visit__referrer_id visit_buy_conversion_rate +------------------- -------------------- --------------------------- +2020-01-01T00:00:00 fb_ad_1 2 +2020-01-02T00:00:00 fb_ad_1 1 +2020-01-03T00:00:00 fb_ad_1 1 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/DuckDB/test_conversion_metric_with_filter_not_in_group_by__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/DuckDB/test_conversion_metric_with_filter_not_in_group_by__query_output.txt new file mode 100644 index 0000000000..e3046748a3 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/DuckDB/test_conversion_metric_with_filter_not_in_group_by__query_output.txt @@ -0,0 +1,3 @@ + visit_buy_conversions +----------------------- + 3 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/DuckDB/test_conversion_metric_with_time_constraint__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/DuckDB/test_conversion_metric_with_time_constraint__query_output.txt new file mode 100644 index 0000000000..3fb6f042e8 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/DuckDB/test_conversion_metric_with_time_constraint__query_output.txt @@ -0,0 +1,3 @@ +visit__referrer_id visit_buy_conversion_rate +-------------------- --------------------------- +fb_ad_1 1.5 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/DuckDB/test_conversion_metric_with_window__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/DuckDB/test_conversion_metric_with_window__query_output.txt new file mode 100644 index 0000000000..294fbf9259 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/DuckDB/test_conversion_metric_with_window__query_output.txt @@ -0,0 +1,10 @@ +metric_time__day visit_buy_conversion_rate_7days +------------------- --------------------------------- +2020-01-01T00:00:00 0.50 +2020-01-02T00:00:00 0.50 +2020-01-03T00:00:00 0.67 +2020-01-04T00:00:00 None +2020-01-06T00:00:00 None +2020-01-07T00:00:00 1.00 +2020-01-10T00:00:00 1.00 +2020-01-12T00:00:00 None diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/DuckDB/test_conversion_metric_with_window_and_time_constraint__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/DuckDB/test_conversion_metric_with_window_and_time_constraint__query_output.txt new file mode 100644 index 0000000000..b4958e8622 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/DuckDB/test_conversion_metric_with_window_and_time_constraint__query_output.txt @@ -0,0 +1,4 @@ +metric_time__day visit__referrer_id visit_buy_conversion_rate_7days +------------------- -------------------- --------------------------------- +2020-01-01T00:00:00 fb_ad_1 2 +2020-01-02T00:00:00 fb_ad_1 1 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Postgres/test_conversion_metric__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Postgres/test_conversion_metric__query_output.txt new file mode 100644 index 0000000000..d59c3ac1da --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Postgres/test_conversion_metric__query_output.txt @@ -0,0 +1,10 @@ +metric_time__day visit_buy_conversion_rate +------------------- --------------------------- +2020-01-01T00:00:00 0.50 +2020-01-02T00:00:00 0.50 +2020-01-03T00:00:00 0.67 +2020-01-04T00:00:00 None +2020-01-06T00:00:00 None +2020-01-07T00:00:00 1.00 +2020-01-10T00:00:00 1.00 +2020-01-12T00:00:00 None diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Postgres/test_conversion_metric_with_categorical_filter__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Postgres/test_conversion_metric_with_categorical_filter__query_output.txt new file mode 100644 index 0000000000..e1e5bd822d --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Postgres/test_conversion_metric_with_categorical_filter__query_output.txt @@ -0,0 +1,5 @@ +metric_time__day visit__referrer_id visit_buy_conversion_rate +------------------- -------------------- --------------------------- +2020-01-01T00:00:00 fb_ad_1 2 +2020-01-02T00:00:00 fb_ad_1 1 +2020-01-03T00:00:00 fb_ad_1 1 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Postgres/test_conversion_metric_with_filter_not_in_group_by__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Postgres/test_conversion_metric_with_filter_not_in_group_by__query_output.txt new file mode 100644 index 0000000000..e3046748a3 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Postgres/test_conversion_metric_with_filter_not_in_group_by__query_output.txt @@ -0,0 +1,3 @@ + visit_buy_conversions +----------------------- + 3 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Postgres/test_conversion_metric_with_time_constraint__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Postgres/test_conversion_metric_with_time_constraint__query_output.txt new file mode 100644 index 0000000000..3fb6f042e8 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Postgres/test_conversion_metric_with_time_constraint__query_output.txt @@ -0,0 +1,3 @@ +visit__referrer_id visit_buy_conversion_rate +-------------------- --------------------------- +fb_ad_1 1.5 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Postgres/test_conversion_metric_with_window__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Postgres/test_conversion_metric_with_window__query_output.txt new file mode 100644 index 0000000000..294fbf9259 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Postgres/test_conversion_metric_with_window__query_output.txt @@ -0,0 +1,10 @@ +metric_time__day visit_buy_conversion_rate_7days +------------------- --------------------------------- +2020-01-01T00:00:00 0.50 +2020-01-02T00:00:00 0.50 +2020-01-03T00:00:00 0.67 +2020-01-04T00:00:00 None +2020-01-06T00:00:00 None +2020-01-07T00:00:00 1.00 +2020-01-10T00:00:00 1.00 +2020-01-12T00:00:00 None diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Postgres/test_conversion_metric_with_window_and_time_constraint__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Postgres/test_conversion_metric_with_window_and_time_constraint__query_output.txt new file mode 100644 index 0000000000..b4958e8622 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Postgres/test_conversion_metric_with_window_and_time_constraint__query_output.txt @@ -0,0 +1,4 @@ +metric_time__day visit__referrer_id visit_buy_conversion_rate_7days +------------------- -------------------- --------------------------------- +2020-01-01T00:00:00 fb_ad_1 2 +2020-01-02T00:00:00 fb_ad_1 1 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Redshift/test_conversion_metric__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Redshift/test_conversion_metric__query_output.txt new file mode 100644 index 0000000000..d59c3ac1da --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Redshift/test_conversion_metric__query_output.txt @@ -0,0 +1,10 @@ +metric_time__day visit_buy_conversion_rate +------------------- --------------------------- +2020-01-01T00:00:00 0.50 +2020-01-02T00:00:00 0.50 +2020-01-03T00:00:00 0.67 +2020-01-04T00:00:00 None +2020-01-06T00:00:00 None +2020-01-07T00:00:00 1.00 +2020-01-10T00:00:00 1.00 +2020-01-12T00:00:00 None diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Redshift/test_conversion_metric_with_categorical_filter__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Redshift/test_conversion_metric_with_categorical_filter__query_output.txt new file mode 100644 index 0000000000..e1e5bd822d --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Redshift/test_conversion_metric_with_categorical_filter__query_output.txt @@ -0,0 +1,5 @@ +metric_time__day visit__referrer_id visit_buy_conversion_rate +------------------- -------------------- --------------------------- +2020-01-01T00:00:00 fb_ad_1 2 +2020-01-02T00:00:00 fb_ad_1 1 +2020-01-03T00:00:00 fb_ad_1 1 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Redshift/test_conversion_metric_with_filter_not_in_group_by__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Redshift/test_conversion_metric_with_filter_not_in_group_by__query_output.txt new file mode 100644 index 0000000000..e3046748a3 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Redshift/test_conversion_metric_with_filter_not_in_group_by__query_output.txt @@ -0,0 +1,3 @@ + visit_buy_conversions +----------------------- + 3 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Redshift/test_conversion_metric_with_time_constraint__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Redshift/test_conversion_metric_with_time_constraint__query_output.txt new file mode 100644 index 0000000000..3fb6f042e8 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Redshift/test_conversion_metric_with_time_constraint__query_output.txt @@ -0,0 +1,3 @@ +visit__referrer_id visit_buy_conversion_rate +-------------------- --------------------------- +fb_ad_1 1.5 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Redshift/test_conversion_metric_with_window__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Redshift/test_conversion_metric_with_window__query_output.txt new file mode 100644 index 0000000000..294fbf9259 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Redshift/test_conversion_metric_with_window__query_output.txt @@ -0,0 +1,10 @@ +metric_time__day visit_buy_conversion_rate_7days +------------------- --------------------------------- +2020-01-01T00:00:00 0.50 +2020-01-02T00:00:00 0.50 +2020-01-03T00:00:00 0.67 +2020-01-04T00:00:00 None +2020-01-06T00:00:00 None +2020-01-07T00:00:00 1.00 +2020-01-10T00:00:00 1.00 +2020-01-12T00:00:00 None diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Redshift/test_conversion_metric_with_window_and_time_constraint__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Redshift/test_conversion_metric_with_window_and_time_constraint__query_output.txt new file mode 100644 index 0000000000..b4958e8622 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Redshift/test_conversion_metric_with_window_and_time_constraint__query_output.txt @@ -0,0 +1,4 @@ +metric_time__day visit__referrer_id visit_buy_conversion_rate_7days +------------------- -------------------- --------------------------------- +2020-01-01T00:00:00 fb_ad_1 2 +2020-01-02T00:00:00 fb_ad_1 1 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Snowflake/test_conversion_metric__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Snowflake/test_conversion_metric__query_output.txt new file mode 100644 index 0000000000..57e6611f5c --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Snowflake/test_conversion_metric__query_output.txt @@ -0,0 +1,10 @@ +METRIC_TIME__DAY VISIT_BUY_CONVERSION_RATE +------------------- --------------------------- +2020-01-01T00:00:00 0.50 +2020-01-02T00:00:00 0.50 +2020-01-03T00:00:00 0.67 +2020-01-04T00:00:00 None +2020-01-06T00:00:00 None +2020-01-07T00:00:00 1.00 +2020-01-10T00:00:00 1.00 +2020-01-12T00:00:00 None diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Snowflake/test_conversion_metric_with_categorical_filter__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Snowflake/test_conversion_metric_with_categorical_filter__query_output.txt new file mode 100644 index 0000000000..c8138396fe --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Snowflake/test_conversion_metric_with_categorical_filter__query_output.txt @@ -0,0 +1,5 @@ +METRIC_TIME__DAY VISIT__REFERRER_ID VISIT_BUY_CONVERSION_RATE +------------------- -------------------- --------------------------- +2020-01-01T00:00:00 fb_ad_1 2 +2020-01-02T00:00:00 fb_ad_1 1 +2020-01-03T00:00:00 fb_ad_1 1 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Snowflake/test_conversion_metric_with_filter_not_in_group_by__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Snowflake/test_conversion_metric_with_filter_not_in_group_by__query_output.txt new file mode 100644 index 0000000000..9cbe5f4439 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Snowflake/test_conversion_metric_with_filter_not_in_group_by__query_output.txt @@ -0,0 +1,3 @@ + VISIT_BUY_CONVERSIONS +----------------------- + 3 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Snowflake/test_conversion_metric_with_time_constraint__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Snowflake/test_conversion_metric_with_time_constraint__query_output.txt new file mode 100644 index 0000000000..53b4574200 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Snowflake/test_conversion_metric_with_time_constraint__query_output.txt @@ -0,0 +1,3 @@ +VISIT__REFERRER_ID VISIT_BUY_CONVERSION_RATE +-------------------- --------------------------- +fb_ad_1 1.5 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Snowflake/test_conversion_metric_with_window__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Snowflake/test_conversion_metric_with_window__query_output.txt new file mode 100644 index 0000000000..1d3f1dd15a --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Snowflake/test_conversion_metric_with_window__query_output.txt @@ -0,0 +1,10 @@ +METRIC_TIME__DAY VISIT_BUY_CONVERSION_RATE_7DAYS +------------------- --------------------------------- +2020-01-01T00:00:00 0.50 +2020-01-02T00:00:00 0.50 +2020-01-03T00:00:00 0.67 +2020-01-04T00:00:00 None +2020-01-06T00:00:00 None +2020-01-07T00:00:00 1.00 +2020-01-10T00:00:00 1.00 +2020-01-12T00:00:00 None diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Snowflake/test_conversion_metric_with_window_and_time_constraint__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Snowflake/test_conversion_metric_with_window_and_time_constraint__query_output.txt new file mode 100644 index 0000000000..fd1601ac58 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Snowflake/test_conversion_metric_with_window_and_time_constraint__query_output.txt @@ -0,0 +1,4 @@ +METRIC_TIME__DAY VISIT__REFERRER_ID VISIT_BUY_CONVERSION_RATE_7DAYS +------------------- -------------------- --------------------------------- +2020-01-01T00:00:00 fb_ad_1 2 +2020-01-02T00:00:00 fb_ad_1 1 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Trino/test_conversion_metric__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Trino/test_conversion_metric__query_output.txt new file mode 100644 index 0000000000..d59c3ac1da --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Trino/test_conversion_metric__query_output.txt @@ -0,0 +1,10 @@ +metric_time__day visit_buy_conversion_rate +------------------- --------------------------- +2020-01-01T00:00:00 0.50 +2020-01-02T00:00:00 0.50 +2020-01-03T00:00:00 0.67 +2020-01-04T00:00:00 None +2020-01-06T00:00:00 None +2020-01-07T00:00:00 1.00 +2020-01-10T00:00:00 1.00 +2020-01-12T00:00:00 None diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Trino/test_conversion_metric_with_categorical_filter__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Trino/test_conversion_metric_with_categorical_filter__query_output.txt new file mode 100644 index 0000000000..e1e5bd822d --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Trino/test_conversion_metric_with_categorical_filter__query_output.txt @@ -0,0 +1,5 @@ +metric_time__day visit__referrer_id visit_buy_conversion_rate +------------------- -------------------- --------------------------- +2020-01-01T00:00:00 fb_ad_1 2 +2020-01-02T00:00:00 fb_ad_1 1 +2020-01-03T00:00:00 fb_ad_1 1 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Trino/test_conversion_metric_with_filter_not_in_group_by__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Trino/test_conversion_metric_with_filter_not_in_group_by__query_output.txt new file mode 100644 index 0000000000..e3046748a3 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Trino/test_conversion_metric_with_filter_not_in_group_by__query_output.txt @@ -0,0 +1,3 @@ + visit_buy_conversions +----------------------- + 3 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Trino/test_conversion_metric_with_time_constraint__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Trino/test_conversion_metric_with_time_constraint__query_output.txt new file mode 100644 index 0000000000..3fb6f042e8 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Trino/test_conversion_metric_with_time_constraint__query_output.txt @@ -0,0 +1,3 @@ +visit__referrer_id visit_buy_conversion_rate +-------------------- --------------------------- +fb_ad_1 1.5 diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Trino/test_conversion_metric_with_window__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Trino/test_conversion_metric_with_window__query_output.txt new file mode 100644 index 0000000000..294fbf9259 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Trino/test_conversion_metric_with_window__query_output.txt @@ -0,0 +1,10 @@ +metric_time__day visit_buy_conversion_rate_7days +------------------- --------------------------------- +2020-01-01T00:00:00 0.50 +2020-01-02T00:00:00 0.50 +2020-01-03T00:00:00 0.67 +2020-01-04T00:00:00 None +2020-01-06T00:00:00 None +2020-01-07T00:00:00 1.00 +2020-01-10T00:00:00 1.00 +2020-01-12T00:00:00 None diff --git a/tests_metricflow/snapshots/test_conversion_metrics.py/str/Trino/test_conversion_metric_with_window_and_time_constraint__query_output.txt b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Trino/test_conversion_metric_with_window_and_time_constraint__query_output.txt new file mode 100644 index 0000000000..b4958e8622 --- /dev/null +++ b/tests_metricflow/snapshots/test_conversion_metrics.py/str/Trino/test_conversion_metric_with_window_and_time_constraint__query_output.txt @@ -0,0 +1,4 @@ +metric_time__day visit__referrer_id visit_buy_conversion_rate_7days +------------------- -------------------- --------------------------------- +2020-01-01T00:00:00 fb_ad_1 2 +2020-01-02T00:00:00 fb_ad_1 1 diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_optimizer.py/DataflowPlan/test_conversion_metric_predicate_pushdown__dfp_0.xml b/tests_metricflow/snapshots/test_predicate_pushdown_optimizer.py/DataflowPlan/test_conversion_metric_predicate_pushdown__dfp_0.xml index db32699106..d3b4aa90d8 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_optimizer.py/DataflowPlan/test_conversion_metric_predicate_pushdown__dfp_0.xml +++ b/tests_metricflow/snapshots/test_predicate_pushdown_optimizer.py/DataflowPlan/test_conversion_metric_predicate_pushdown__dfp_0.xml @@ -202,7 +202,7 @@ - + @@ -215,144 +215,141 @@ - - - - - - - - - + + + + + + + + + + + + + + + + - - - - + + + + + + + + + + + + + + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - - + + + - - - - - - - - - - - - - - + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_optimizer.py/DataflowPlan/test_conversion_metric_predicate_pushdown__dfpo_0.xml b/tests_metricflow/snapshots/test_predicate_pushdown_optimizer.py/DataflowPlan/test_conversion_metric_predicate_pushdown__dfpo_0.xml index 116896e916..00d3e1a08e 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_optimizer.py/DataflowPlan/test_conversion_metric_predicate_pushdown__dfpo_0.xml +++ b/tests_metricflow/snapshots/test_predicate_pushdown_optimizer.py/DataflowPlan/test_conversion_metric_predicate_pushdown__dfpo_0.xml @@ -62,7 +62,7 @@ - + @@ -79,7 +79,7 @@ - + @@ -99,10 +99,10 @@ - - + + - + @@ -110,7 +110,7 @@ - + @@ -126,7 +126,7 @@ - + @@ -180,7 +180,7 @@ - + @@ -199,7 +199,7 @@ - + @@ -212,144 +212,141 @@ - - - - - - - - - + + + + + + + + + + + + + + + + - - - - + + + + + + + + + + + + + + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - - + + + - - - - - - - - - - - - - - + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_query_filters__plan0.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_query_filters__plan0.sql index 485da5fb9b..d294d3a7a3 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_query_filters__plan0.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_query_filters__plan0.sql @@ -1,15 +1,15 @@ -- Compute Metrics via Expressions SELECT - subq_27.metric_time__day - , subq_27.user__home_state_latest - , CAST(subq_27.buys AS FLOAT64) / CAST(NULLIF(subq_27.visits, 0) AS FLOAT64) AS visit_buy_conversion_rate_7days + subq_24.metric_time__day + , subq_24.user__home_state_latest + , CAST(subq_24.buys AS FLOAT64) / CAST(NULLIF(subq_24.visits, 0) AS FLOAT64) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_9.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , COALESCE(subq_9.user__home_state_latest, subq_26.user__home_state_latest) AS user__home_state_latest + COALESCE(subq_9.metric_time__day, subq_23.metric_time__day) AS metric_time__day + , COALESCE(subq_9.user__home_state_latest, subq_23.user__home_state_latest) AS user__home_state_latest , MAX(subq_9.visits) AS visits - , MAX(subq_26.buys) AS buys + , MAX(subq_23.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -182,451 +182,443 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_25.metric_time__day - , subq_25.user__home_state_latest - , SUM(subq_25.buys) AS buys + subq_22.metric_time__day + , subq_22.user__home_state_latest + , SUM(subq_22.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'user__home_state_latest', 'metric_time__day'] SELECT - subq_24.metric_time__day - , subq_24.user__home_state_latest - , subq_24.buys + subq_21.metric_time__day + , subq_21.user__home_state_latest + , subq_21.buys FROM ( - -- Join Standard Outputs + -- Find conversions for user within the range of 7 day SELECT - subq_21.metric_time__day AS metric_time__day - , subq_21.user AS user - , subq_21.visit__referrer_id AS visit__referrer_id - , subq_23.home_state_latest AS user__home_state_latest - , subq_21.buys AS buys + subq_20.ds__day + , subq_20.metric_time__day + , subq_20.user + , subq_20.visit__referrer_id + , subq_20.user__home_state_latest + , subq_20.buys + , subq_20.visits FROM ( - -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day', 'user'] - SELECT - subq_20.metric_time__day - , subq_20.user - , subq_20.visit__referrer_id - , subq_20.buys + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_16.visits) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_16.visit__referrer_id) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visit__referrer_id + , FIRST_VALUE(subq_16.user__home_state_latest) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user__home_state_latest + , FIRST_VALUE(subq_16.ds__day) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_16.metric_time__day) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS metric_time__day + , FIRST_VALUE(subq_16.user) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_19.mf_internal_uuid AS mf_internal_uuid + , subq_19.buys AS buys FROM ( - -- Find conversions for user within the range of 7 day + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'user__home_state_latest', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_19.ds__day - , subq_19.metric_time__day - , subq_19.user - , subq_19.visit__referrer_id - , subq_19.user__home_state_latest - , subq_19.buys - , subq_19.visits + subq_15.ds__day + , subq_15.metric_time__day + , subq_15.user + , subq_15.visit__referrer_id + , subq_15.user__home_state_latest + , subq_15.visits FROM ( - -- Dedupe the fanout with mf_internal_uuid in the conversion data set - SELECT DISTINCT - FIRST_VALUE(subq_15.visits) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS visits - , FIRST_VALUE(subq_15.visit__referrer_id) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS visit__referrer_id - , FIRST_VALUE(subq_15.user__home_state_latest) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS user__home_state_latest - , FIRST_VALUE(subq_15.ds__day) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS ds__day - , FIRST_VALUE(subq_15.metric_time__day) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS metric_time__day - , FIRST_VALUE(subq_15.user) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS user - , subq_18.mf_internal_uuid AS mf_internal_uuid - , subq_18.buys AS buys + -- Constrain Output with WHERE + SELECT + subq_14.ds__day + , subq_14.ds__week + , subq_14.ds__month + , subq_14.ds__quarter + , subq_14.ds__year + , subq_14.ds__extract_year + , subq_14.ds__extract_quarter + , subq_14.ds__extract_month + , subq_14.ds__extract_day + , subq_14.ds__extract_dow + , subq_14.ds__extract_doy + , subq_14.visit__ds__day + , subq_14.visit__ds__week + , subq_14.visit__ds__month + , subq_14.visit__ds__quarter + , subq_14.visit__ds__year + , subq_14.visit__ds__extract_year + , subq_14.visit__ds__extract_quarter + , subq_14.visit__ds__extract_month + , subq_14.visit__ds__extract_day + , subq_14.visit__ds__extract_dow + , subq_14.visit__ds__extract_doy + , subq_14.metric_time__day + , subq_14.metric_time__week + , subq_14.metric_time__month + , subq_14.metric_time__quarter + , subq_14.metric_time__year + , subq_14.metric_time__extract_year + , subq_14.metric_time__extract_quarter + , subq_14.metric_time__extract_month + , subq_14.metric_time__extract_day + , subq_14.metric_time__extract_dow + , subq_14.metric_time__extract_doy + , subq_14.user + , subq_14.session + , subq_14.visit__user + , subq_14.visit__session + , subq_14.referrer_id + , subq_14.visit__referrer_id + , subq_14.user__home_state_latest + , subq_14.visits + , subq_14.visitors FROM ( - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'user__home_state_latest', 'ds__day', 'metric_time__day', 'user'] + -- Join Standard Outputs SELECT - subq_14.ds__day - , subq_14.metric_time__day - , subq_14.user - , subq_14.visit__referrer_id - , subq_14.user__home_state_latest - , subq_14.visits + subq_11.ds__day AS ds__day + , subq_11.ds__week AS ds__week + , subq_11.ds__month AS ds__month + , subq_11.ds__quarter AS ds__quarter + , subq_11.ds__year AS ds__year + , subq_11.ds__extract_year AS ds__extract_year + , subq_11.ds__extract_quarter AS ds__extract_quarter + , subq_11.ds__extract_month AS ds__extract_month + , subq_11.ds__extract_day AS ds__extract_day + , subq_11.ds__extract_dow AS ds__extract_dow + , subq_11.ds__extract_doy AS ds__extract_doy + , subq_11.visit__ds__day AS visit__ds__day + , subq_11.visit__ds__week AS visit__ds__week + , subq_11.visit__ds__month AS visit__ds__month + , subq_11.visit__ds__quarter AS visit__ds__quarter + , subq_11.visit__ds__year AS visit__ds__year + , subq_11.visit__ds__extract_year AS visit__ds__extract_year + , subq_11.visit__ds__extract_quarter AS visit__ds__extract_quarter + , subq_11.visit__ds__extract_month AS visit__ds__extract_month + , subq_11.visit__ds__extract_day AS visit__ds__extract_day + , subq_11.visit__ds__extract_dow AS visit__ds__extract_dow + , subq_11.visit__ds__extract_doy AS visit__ds__extract_doy + , subq_11.metric_time__day AS metric_time__day + , subq_11.metric_time__week AS metric_time__week + , subq_11.metric_time__month AS metric_time__month + , subq_11.metric_time__quarter AS metric_time__quarter + , subq_11.metric_time__year AS metric_time__year + , subq_11.metric_time__extract_year AS metric_time__extract_year + , subq_11.metric_time__extract_quarter AS metric_time__extract_quarter + , subq_11.metric_time__extract_month AS metric_time__extract_month + , subq_11.metric_time__extract_day AS metric_time__extract_day + , subq_11.metric_time__extract_dow AS metric_time__extract_dow + , subq_11.metric_time__extract_doy AS metric_time__extract_doy + , subq_11.user AS user + , subq_11.session AS session + , subq_11.visit__user AS visit__user + , subq_11.visit__session AS visit__session + , subq_11.referrer_id AS referrer_id + , subq_11.visit__referrer_id AS visit__referrer_id + , subq_13.home_state_latest AS user__home_state_latest + , subq_11.visits AS visits + , subq_11.visitors AS visitors FROM ( - -- Join Standard Outputs + -- Metric Time Dimension 'ds' SELECT - subq_11.ds__day AS ds__day - , subq_11.ds__week AS ds__week - , subq_11.ds__month AS ds__month - , subq_11.ds__quarter AS ds__quarter - , subq_11.ds__year AS ds__year - , subq_11.ds__extract_year AS ds__extract_year - , subq_11.ds__extract_quarter AS ds__extract_quarter - , subq_11.ds__extract_month AS ds__extract_month - , subq_11.ds__extract_day AS ds__extract_day - , subq_11.ds__extract_dow AS ds__extract_dow - , subq_11.ds__extract_doy AS ds__extract_doy - , subq_11.visit__ds__day AS visit__ds__day - , subq_11.visit__ds__week AS visit__ds__week - , subq_11.visit__ds__month AS visit__ds__month - , subq_11.visit__ds__quarter AS visit__ds__quarter - , subq_11.visit__ds__year AS visit__ds__year - , subq_11.visit__ds__extract_year AS visit__ds__extract_year - , subq_11.visit__ds__extract_quarter AS visit__ds__extract_quarter - , subq_11.visit__ds__extract_month AS visit__ds__extract_month - , subq_11.visit__ds__extract_day AS visit__ds__extract_day - , subq_11.visit__ds__extract_dow AS visit__ds__extract_dow - , subq_11.visit__ds__extract_doy AS visit__ds__extract_doy - , subq_11.metric_time__day AS metric_time__day - , subq_11.metric_time__week AS metric_time__week - , subq_11.metric_time__month AS metric_time__month - , subq_11.metric_time__quarter AS metric_time__quarter - , subq_11.metric_time__year AS metric_time__year - , subq_11.metric_time__extract_year AS metric_time__extract_year - , subq_11.metric_time__extract_quarter AS metric_time__extract_quarter - , subq_11.metric_time__extract_month AS metric_time__extract_month - , subq_11.metric_time__extract_day AS metric_time__extract_day - , subq_11.metric_time__extract_dow AS metric_time__extract_dow - , subq_11.metric_time__extract_doy AS metric_time__extract_doy - , subq_11.user AS user - , subq_11.session AS session - , subq_11.visit__user AS visit__user - , subq_11.visit__session AS visit__session - , subq_11.referrer_id AS referrer_id - , subq_11.visit__referrer_id AS visit__referrer_id - , subq_13.home_state_latest AS user__home_state_latest - , subq_11.visits AS visits - , subq_11.visitors AS visitors + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.visit__ds__day + , subq_10.visit__ds__week + , subq_10.visit__ds__month + , subq_10.visit__ds__quarter + , subq_10.visit__ds__year + , subq_10.visit__ds__extract_year + , subq_10.visit__ds__extract_quarter + , subq_10.visit__ds__extract_month + , subq_10.visit__ds__extract_day + , subq_10.visit__ds__extract_dow + , subq_10.visit__ds__extract_doy + , subq_10.ds__day AS metric_time__day + , subq_10.ds__week AS metric_time__week + , subq_10.ds__month AS metric_time__month + , subq_10.ds__quarter AS metric_time__quarter + , subq_10.ds__year AS metric_time__year + , subq_10.ds__extract_year AS metric_time__extract_year + , subq_10.ds__extract_quarter AS metric_time__extract_quarter + , subq_10.ds__extract_month AS metric_time__extract_month + , subq_10.ds__extract_day AS metric_time__extract_day + , subq_10.ds__extract_dow AS metric_time__extract_dow + , subq_10.ds__extract_doy AS metric_time__extract_doy + , subq_10.user + , subq_10.session + , subq_10.visit__user + , subq_10.visit__session + , subq_10.referrer_id + , subq_10.visit__referrer_id + , subq_10.visits + , subq_10.visitors FROM ( - -- Metric Time Dimension 'ds' - SELECT - subq_10.ds__day - , subq_10.ds__week - , subq_10.ds__month - , subq_10.ds__quarter - , subq_10.ds__year - , subq_10.ds__extract_year - , subq_10.ds__extract_quarter - , subq_10.ds__extract_month - , subq_10.ds__extract_day - , subq_10.ds__extract_dow - , subq_10.ds__extract_doy - , subq_10.visit__ds__day - , subq_10.visit__ds__week - , subq_10.visit__ds__month - , subq_10.visit__ds__quarter - , subq_10.visit__ds__year - , subq_10.visit__ds__extract_year - , subq_10.visit__ds__extract_quarter - , subq_10.visit__ds__extract_month - , subq_10.visit__ds__extract_day - , subq_10.visit__ds__extract_dow - , subq_10.visit__ds__extract_doy - , subq_10.ds__day AS metric_time__day - , subq_10.ds__week AS metric_time__week - , subq_10.ds__month AS metric_time__month - , subq_10.ds__quarter AS metric_time__quarter - , subq_10.ds__year AS metric_time__year - , subq_10.ds__extract_year AS metric_time__extract_year - , subq_10.ds__extract_quarter AS metric_time__extract_quarter - , subq_10.ds__extract_month AS metric_time__extract_month - , subq_10.ds__extract_day AS metric_time__extract_day - , subq_10.ds__extract_dow AS metric_time__extract_dow - , subq_10.ds__extract_doy AS metric_time__extract_doy - , subq_10.user - , subq_10.session - , subq_10.visit__user - , subq_10.visit__session - , subq_10.referrer_id - , subq_10.visit__referrer_id - , subq_10.visits - , subq_10.visitors - FROM ( - -- Read Elements From Semantic Model 'visits_source' - SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS ds__day - , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS ds__week - , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS ds__month - , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS ds__quarter - , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS ds__extract_dow - , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS visit__ds__day - , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS visit__ds__week - , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS visit__ds__month - , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS visit__ds__quarter - , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS visit__ds__extract_dow - , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_10 - ) subq_11 - LEFT OUTER JOIN ( - -- Pass Only Elements: ['home_state_latest', 'user'] + -- Read Elements From Semantic Model 'visits_source' SELECT - subq_12.user - , subq_12.home_state_latest - FROM ( - -- Read Elements From Semantic Model 'users_latest' - SELECT - DATETIME_TRUNC(users_latest_src_28000.ds, day) AS ds_latest__day - , DATETIME_TRUNC(users_latest_src_28000.ds, isoweek) AS ds_latest__week - , DATETIME_TRUNC(users_latest_src_28000.ds, month) AS ds_latest__month - , DATETIME_TRUNC(users_latest_src_28000.ds, quarter) AS ds_latest__quarter - , DATETIME_TRUNC(users_latest_src_28000.ds, year) AS ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS ds_latest__extract_day - , IF(EXTRACT(dayofweek FROM users_latest_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM users_latest_src_28000.ds) - 1) AS ds_latest__extract_dow - , EXTRACT(dayofyear FROM users_latest_src_28000.ds) AS ds_latest__extract_doy - , users_latest_src_28000.home_state_latest - , DATETIME_TRUNC(users_latest_src_28000.ds, day) AS user__ds_latest__day - , DATETIME_TRUNC(users_latest_src_28000.ds, isoweek) AS user__ds_latest__week - , DATETIME_TRUNC(users_latest_src_28000.ds, month) AS user__ds_latest__month - , DATETIME_TRUNC(users_latest_src_28000.ds, quarter) AS user__ds_latest__quarter - , DATETIME_TRUNC(users_latest_src_28000.ds, year) AS user__ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS user__ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS user__ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS user__ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS user__ds_latest__extract_day - , IF(EXTRACT(dayofweek FROM users_latest_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM users_latest_src_28000.ds) - 1) AS user__ds_latest__extract_dow - , EXTRACT(dayofyear FROM users_latest_src_28000.ds) AS user__ds_latest__extract_doy - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , users_latest_src_28000.user_id AS user - FROM ***************************.dim_users_latest users_latest_src_28000 - ) subq_12 - ) subq_13 - ON - subq_11.user = subq_13.user - ) subq_14 - ) subq_15 - INNER JOIN ( - -- Add column with generated UUID - SELECT - subq_17.ds__day - , subq_17.ds__week - , subq_17.ds__month - , subq_17.ds__quarter - , subq_17.ds__year - , subq_17.ds__extract_year - , subq_17.ds__extract_quarter - , subq_17.ds__extract_month - , subq_17.ds__extract_day - , subq_17.ds__extract_dow - , subq_17.ds__extract_doy - , subq_17.buy__ds__day - , subq_17.buy__ds__week - , subq_17.buy__ds__month - , subq_17.buy__ds__quarter - , subq_17.buy__ds__year - , subq_17.buy__ds__extract_year - , subq_17.buy__ds__extract_quarter - , subq_17.buy__ds__extract_month - , subq_17.buy__ds__extract_day - , subq_17.buy__ds__extract_dow - , subq_17.buy__ds__extract_doy - , subq_17.metric_time__day - , subq_17.metric_time__week - , subq_17.metric_time__month - , subq_17.metric_time__quarter - , subq_17.metric_time__year - , subq_17.metric_time__extract_year - , subq_17.metric_time__extract_quarter - , subq_17.metric_time__extract_month - , subq_17.metric_time__extract_day - , subq_17.metric_time__extract_dow - , subq_17.metric_time__extract_doy - , subq_17.user - , subq_17.session_id - , subq_17.buy__user - , subq_17.buy__session_id - , subq_17.buys - , subq_17.buyers - , GENERATE_UUID() AS mf_internal_uuid - FROM ( - -- Metric Time Dimension 'ds' + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS ds__day + , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS ds__week + , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS ds__month + , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS ds__quarter + , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATETIME_TRUNC(visits_source_src_28000.ds, day) AS visit__ds__day + , DATETIME_TRUNC(visits_source_src_28000.ds, isoweek) AS visit__ds__week + , DATETIME_TRUNC(visits_source_src_28000.ds, month) AS visit__ds__month + , DATETIME_TRUNC(visits_source_src_28000.ds, quarter) AS visit__ds__quarter + , DATETIME_TRUNC(visits_source_src_28000.ds, year) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_28000.ds) - 1) AS visit__ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_10 + ) subq_11 + LEFT OUTER JOIN ( + -- Pass Only Elements: ['home_state_latest', 'user'] SELECT - subq_16.ds__day - , subq_16.ds__week - , subq_16.ds__month - , subq_16.ds__quarter - , subq_16.ds__year - , subq_16.ds__extract_year - , subq_16.ds__extract_quarter - , subq_16.ds__extract_month - , subq_16.ds__extract_day - , subq_16.ds__extract_dow - , subq_16.ds__extract_doy - , subq_16.buy__ds__day - , subq_16.buy__ds__week - , subq_16.buy__ds__month - , subq_16.buy__ds__quarter - , subq_16.buy__ds__year - , subq_16.buy__ds__extract_year - , subq_16.buy__ds__extract_quarter - , subq_16.buy__ds__extract_month - , subq_16.buy__ds__extract_day - , subq_16.buy__ds__extract_dow - , subq_16.buy__ds__extract_doy - , subq_16.ds__day AS metric_time__day - , subq_16.ds__week AS metric_time__week - , subq_16.ds__month AS metric_time__month - , subq_16.ds__quarter AS metric_time__quarter - , subq_16.ds__year AS metric_time__year - , subq_16.ds__extract_year AS metric_time__extract_year - , subq_16.ds__extract_quarter AS metric_time__extract_quarter - , subq_16.ds__extract_month AS metric_time__extract_month - , subq_16.ds__extract_day AS metric_time__extract_day - , subq_16.ds__extract_dow AS metric_time__extract_dow - , subq_16.ds__extract_doy AS metric_time__extract_doy - , subq_16.user - , subq_16.session_id - , subq_16.buy__user - , subq_16.buy__session_id - , subq_16.buys - , subq_16.buyers + subq_12.user + , subq_12.home_state_latest FROM ( - -- Read Elements From Semantic Model 'buys_source' + -- Read Elements From Semantic Model 'users_latest' SELECT - 1 AS buys - , buys_source_src_28000.user_id AS buyers - , DATETIME_TRUNC(buys_source_src_28000.ds, day) AS ds__day - , DATETIME_TRUNC(buys_source_src_28000.ds, isoweek) AS ds__week - , DATETIME_TRUNC(buys_source_src_28000.ds, month) AS ds__month - , DATETIME_TRUNC(buys_source_src_28000.ds, quarter) AS ds__quarter - , DATETIME_TRUNC(buys_source_src_28000.ds, year) AS ds__year - , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day - , IF(EXTRACT(dayofweek FROM buys_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM buys_source_src_28000.ds) - 1) AS ds__extract_dow - , EXTRACT(dayofyear FROM buys_source_src_28000.ds) AS ds__extract_doy - , DATETIME_TRUNC(buys_source_src_28000.ds, day) AS buy__ds__day - , DATETIME_TRUNC(buys_source_src_28000.ds, isoweek) AS buy__ds__week - , DATETIME_TRUNC(buys_source_src_28000.ds, month) AS buy__ds__month - , DATETIME_TRUNC(buys_source_src_28000.ds, quarter) AS buy__ds__quarter - , DATETIME_TRUNC(buys_source_src_28000.ds, year) AS buy__ds__year - , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year - , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter - , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month - , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day - , IF(EXTRACT(dayofweek FROM buys_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM buys_source_src_28000.ds) - 1) AS buy__ds__extract_dow - , EXTRACT(dayofyear FROM buys_source_src_28000.ds) AS buy__ds__extract_doy - , buys_source_src_28000.user_id AS user - , buys_source_src_28000.session_id - , buys_source_src_28000.user_id AS buy__user - , buys_source_src_28000.session_id AS buy__session_id - FROM ***************************.fct_buys buys_source_src_28000 - ) subq_16 - ) subq_17 - ) subq_18 - ON - ( - subq_15.user = subq_18.user - ) AND ( - ( - subq_15.ds__day <= subq_18.ds__day - ) AND ( - subq_15.ds__day > DATE_SUB(CAST(subq_18.ds__day AS DATETIME), INTERVAL 7 day) - ) - ) - ) subq_19 - ) subq_20 - ) subq_21 - LEFT OUTER JOIN ( - -- Pass Only Elements: ['home_state_latest', 'user'] - SELECT - subq_22.user - , subq_22.home_state_latest - FROM ( - -- Read Elements From Semantic Model 'users_latest' + DATETIME_TRUNC(users_latest_src_28000.ds, day) AS ds_latest__day + , DATETIME_TRUNC(users_latest_src_28000.ds, isoweek) AS ds_latest__week + , DATETIME_TRUNC(users_latest_src_28000.ds, month) AS ds_latest__month + , DATETIME_TRUNC(users_latest_src_28000.ds, quarter) AS ds_latest__quarter + , DATETIME_TRUNC(users_latest_src_28000.ds, year) AS ds_latest__year + , EXTRACT(year FROM users_latest_src_28000.ds) AS ds_latest__extract_year + , EXTRACT(quarter FROM users_latest_src_28000.ds) AS ds_latest__extract_quarter + , EXTRACT(month FROM users_latest_src_28000.ds) AS ds_latest__extract_month + , EXTRACT(day FROM users_latest_src_28000.ds) AS ds_latest__extract_day + , IF(EXTRACT(dayofweek FROM users_latest_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM users_latest_src_28000.ds) - 1) AS ds_latest__extract_dow + , EXTRACT(dayofyear FROM users_latest_src_28000.ds) AS ds_latest__extract_doy + , users_latest_src_28000.home_state_latest + , DATETIME_TRUNC(users_latest_src_28000.ds, day) AS user__ds_latest__day + , DATETIME_TRUNC(users_latest_src_28000.ds, isoweek) AS user__ds_latest__week + , DATETIME_TRUNC(users_latest_src_28000.ds, month) AS user__ds_latest__month + , DATETIME_TRUNC(users_latest_src_28000.ds, quarter) AS user__ds_latest__quarter + , DATETIME_TRUNC(users_latest_src_28000.ds, year) AS user__ds_latest__year + , EXTRACT(year FROM users_latest_src_28000.ds) AS user__ds_latest__extract_year + , EXTRACT(quarter FROM users_latest_src_28000.ds) AS user__ds_latest__extract_quarter + , EXTRACT(month FROM users_latest_src_28000.ds) AS user__ds_latest__extract_month + , EXTRACT(day FROM users_latest_src_28000.ds) AS user__ds_latest__extract_day + , IF(EXTRACT(dayofweek FROM users_latest_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM users_latest_src_28000.ds) - 1) AS user__ds_latest__extract_dow + , EXTRACT(dayofyear FROM users_latest_src_28000.ds) AS user__ds_latest__extract_doy + , users_latest_src_28000.home_state_latest AS user__home_state_latest + , users_latest_src_28000.user_id AS user + FROM ***************************.dim_users_latest users_latest_src_28000 + ) subq_12 + ) subq_13 + ON + subq_11.user = subq_13.user + ) subq_14 + WHERE visit__referrer_id = '123456' + ) subq_15 + ) subq_16 + INNER JOIN ( + -- Add column with generated UUID SELECT - DATETIME_TRUNC(users_latest_src_28000.ds, day) AS ds_latest__day - , DATETIME_TRUNC(users_latest_src_28000.ds, isoweek) AS ds_latest__week - , DATETIME_TRUNC(users_latest_src_28000.ds, month) AS ds_latest__month - , DATETIME_TRUNC(users_latest_src_28000.ds, quarter) AS ds_latest__quarter - , DATETIME_TRUNC(users_latest_src_28000.ds, year) AS ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS ds_latest__extract_day - , IF(EXTRACT(dayofweek FROM users_latest_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM users_latest_src_28000.ds) - 1) AS ds_latest__extract_dow - , EXTRACT(dayofyear FROM users_latest_src_28000.ds) AS ds_latest__extract_doy - , users_latest_src_28000.home_state_latest - , DATETIME_TRUNC(users_latest_src_28000.ds, day) AS user__ds_latest__day - , DATETIME_TRUNC(users_latest_src_28000.ds, isoweek) AS user__ds_latest__week - , DATETIME_TRUNC(users_latest_src_28000.ds, month) AS user__ds_latest__month - , DATETIME_TRUNC(users_latest_src_28000.ds, quarter) AS user__ds_latest__quarter - , DATETIME_TRUNC(users_latest_src_28000.ds, year) AS user__ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS user__ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS user__ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS user__ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS user__ds_latest__extract_day - , IF(EXTRACT(dayofweek FROM users_latest_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM users_latest_src_28000.ds) - 1) AS user__ds_latest__extract_dow - , EXTRACT(dayofyear FROM users_latest_src_28000.ds) AS user__ds_latest__extract_doy - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , users_latest_src_28000.user_id AS user - FROM ***************************.dim_users_latest users_latest_src_28000 - ) subq_22 - ) subq_23 - ON - subq_21.user = subq_23.user - ) subq_24 - ) subq_25 + subq_18.ds__day + , subq_18.ds__week + , subq_18.ds__month + , subq_18.ds__quarter + , subq_18.ds__year + , subq_18.ds__extract_year + , subq_18.ds__extract_quarter + , subq_18.ds__extract_month + , subq_18.ds__extract_day + , subq_18.ds__extract_dow + , subq_18.ds__extract_doy + , subq_18.buy__ds__day + , subq_18.buy__ds__week + , subq_18.buy__ds__month + , subq_18.buy__ds__quarter + , subq_18.buy__ds__year + , subq_18.buy__ds__extract_year + , subq_18.buy__ds__extract_quarter + , subq_18.buy__ds__extract_month + , subq_18.buy__ds__extract_day + , subq_18.buy__ds__extract_dow + , subq_18.buy__ds__extract_doy + , subq_18.metric_time__day + , subq_18.metric_time__week + , subq_18.metric_time__month + , subq_18.metric_time__quarter + , subq_18.metric_time__year + , subq_18.metric_time__extract_year + , subq_18.metric_time__extract_quarter + , subq_18.metric_time__extract_month + , subq_18.metric_time__extract_day + , subq_18.metric_time__extract_dow + , subq_18.metric_time__extract_doy + , subq_18.user + , subq_18.session_id + , subq_18.buy__user + , subq_18.buy__session_id + , subq_18.buys + , subq_18.buyers + , GENERATE_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_17.ds__day + , subq_17.ds__week + , subq_17.ds__month + , subq_17.ds__quarter + , subq_17.ds__year + , subq_17.ds__extract_year + , subq_17.ds__extract_quarter + , subq_17.ds__extract_month + , subq_17.ds__extract_day + , subq_17.ds__extract_dow + , subq_17.ds__extract_doy + , subq_17.buy__ds__day + , subq_17.buy__ds__week + , subq_17.buy__ds__month + , subq_17.buy__ds__quarter + , subq_17.buy__ds__year + , subq_17.buy__ds__extract_year + , subq_17.buy__ds__extract_quarter + , subq_17.buy__ds__extract_month + , subq_17.buy__ds__extract_day + , subq_17.buy__ds__extract_dow + , subq_17.buy__ds__extract_doy + , subq_17.ds__day AS metric_time__day + , subq_17.ds__week AS metric_time__week + , subq_17.ds__month AS metric_time__month + , subq_17.ds__quarter AS metric_time__quarter + , subq_17.ds__year AS metric_time__year + , subq_17.ds__extract_year AS metric_time__extract_year + , subq_17.ds__extract_quarter AS metric_time__extract_quarter + , subq_17.ds__extract_month AS metric_time__extract_month + , subq_17.ds__extract_day AS metric_time__extract_day + , subq_17.ds__extract_dow AS metric_time__extract_dow + , subq_17.ds__extract_doy AS metric_time__extract_doy + , subq_17.user + , subq_17.session_id + , subq_17.buy__user + , subq_17.buy__session_id + , subq_17.buys + , subq_17.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_28000.user_id AS buyers + , DATETIME_TRUNC(buys_source_src_28000.ds, day) AS ds__day + , DATETIME_TRUNC(buys_source_src_28000.ds, isoweek) AS ds__week + , DATETIME_TRUNC(buys_source_src_28000.ds, month) AS ds__month + , DATETIME_TRUNC(buys_source_src_28000.ds, quarter) AS ds__quarter + , DATETIME_TRUNC(buys_source_src_28000.ds, year) AS ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM buys_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM buys_source_src_28000.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM buys_source_src_28000.ds) AS ds__extract_doy + , DATETIME_TRUNC(buys_source_src_28000.ds, day) AS buy__ds__day + , DATETIME_TRUNC(buys_source_src_28000.ds, isoweek) AS buy__ds__week + , DATETIME_TRUNC(buys_source_src_28000.ds, month) AS buy__ds__month + , DATETIME_TRUNC(buys_source_src_28000.ds, quarter) AS buy__ds__quarter + , DATETIME_TRUNC(buys_source_src_28000.ds, year) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day + , IF(EXTRACT(dayofweek FROM buys_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM buys_source_src_28000.ds) - 1) AS buy__ds__extract_dow + , EXTRACT(dayofyear FROM buys_source_src_28000.ds) AS buy__ds__extract_doy + , buys_source_src_28000.user_id AS user + , buys_source_src_28000.session_id + , buys_source_src_28000.user_id AS buy__user + , buys_source_src_28000.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_17 + ) subq_18 + ) subq_19 + ON + ( + subq_16.user = subq_19.user + ) AND ( + ( + subq_16.ds__day <= subq_19.ds__day + ) AND ( + subq_16.ds__day > DATE_SUB(CAST(subq_19.ds__day AS DATETIME), INTERVAL 7 day) + ) + ) + ) subq_20 + ) subq_21 + ) subq_22 GROUP BY metric_time__day , user__home_state_latest - ) subq_26 + ) subq_23 ON ( - subq_9.user__home_state_latest = subq_26.user__home_state_latest + subq_9.user__home_state_latest = subq_23.user__home_state_latest ) AND ( - subq_9.metric_time__day = subq_26.metric_time__day + subq_9.metric_time__day = subq_23.metric_time__day ) GROUP BY metric_time__day , user__home_state_latest -) subq_27 +) subq_24 diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_query_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_query_filters__plan0_optimized.sql index 2f20ea05f2..6c64ea0451 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_query_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_conversion_metric_query_filters__plan0_optimized.sql @@ -6,10 +6,10 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_37.metric_time__day, subq_54.metric_time__day) AS metric_time__day - , COALESCE(subq_37.user__home_state_latest, subq_54.user__home_state_latest) AS user__home_state_latest - , MAX(subq_37.visits) AS visits - , MAX(subq_54.buys) AS buys + COALESCE(subq_34.metric_time__day, subq_48.metric_time__day) AS metric_time__day + , COALESCE(subq_34.user__home_state_latest, subq_48.user__home_state_latest) AS user__home_state_latest + , MAX(subq_34.visits) AS visits + , MAX(subq_48.buys) AS buys FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'user__home_state_latest', 'metric_time__day'] @@ -22,10 +22,10 @@ FROM ( -- Join Standard Outputs -- Pass Only Elements: ['visits', 'user__home_state_latest', 'visit__referrer_id', 'metric_time__day'] SELECT - subq_30.metric_time__day AS metric_time__day - , subq_30.visit__referrer_id AS visit__referrer_id + subq_27.metric_time__day AS metric_time__day + , subq_27.visit__referrer_id AS visit__referrer_id , users_latest_src_28000.home_state_latest AS user__home_state_latest - , subq_30.visits AS visits + , subq_27.visits AS visits FROM ( -- Read Elements From Semantic Model 'visits_source' -- Metric Time Dimension 'ds' @@ -36,104 +36,115 @@ FROM ( , referrer_id AS visit__referrer_id , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_30 + ) subq_27 LEFT OUTER JOIN ***************************.dim_users_latest users_latest_src_28000 ON - subq_30.user = users_latest_src_28000.user_id - ) subq_34 + subq_27.user = users_latest_src_28000.user_id + ) subq_31 WHERE visit__referrer_id = '123456' GROUP BY metric_time__day , user__home_state_latest - ) subq_37 + ) subq_34 FULL OUTER JOIN ( - -- Join Standard Outputs + -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'user__home_state_latest', 'metric_time__day'] -- Aggregate Measures SELECT - subq_47.metric_time__day AS metric_time__day - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , SUM(subq_47.buys) AS buys + metric_time__day + , user__home_state_latest + , SUM(buys) AS buys FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_43.visits) OVER ( + FIRST_VALUE(subq_41.visits) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_43.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_41.visit__referrer_id) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_43.user__home_state_latest) OVER ( + , FIRST_VALUE(subq_41.user__home_state_latest) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user__home_state_latest - , FIRST_VALUE(subq_43.ds__day) OVER ( + , FIRST_VALUE(subq_41.ds__day) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_43.metric_time__day) OVER ( + , FIRST_VALUE(subq_41.metric_time__day) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_43.user) OVER ( + , FIRST_VALUE(subq_41.user) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_46.mf_internal_uuid AS mf_internal_uuid - , subq_46.buys AS buys + , subq_44.mf_internal_uuid AS mf_internal_uuid + , subq_44.buys AS buys FROM ( - -- Join Standard Outputs + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'user__home_state_latest', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_39.ds__day AS ds__day - , subq_39.metric_time__day AS metric_time__day - , subq_39.user AS user - , subq_39.visit__referrer_id AS visit__referrer_id - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , subq_39.visits AS visits + ds__day + , metric_time__day + , subq_39.user + , visit__referrer_id + , user__home_state_latest + , visits FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - DATETIME_TRUNC(ds, day) AS ds__day - , 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_36.ds__day AS ds__day + , subq_36.metric_time__day AS metric_time__day + , subq_36.user AS user + , subq_36.visit__referrer_id AS visit__referrer_id + , users_latest_src_28000.home_state_latest AS user__home_state_latest + , subq_36.visits AS visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS ds__day + , 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_36 + LEFT OUTER JOIN + ***************************.dim_users_latest users_latest_src_28000 + ON + subq_36.user = users_latest_src_28000.user_id ) subq_39 - LEFT OUTER JOIN - ***************************.dim_users_latest users_latest_src_28000 - ON - subq_39.user = users_latest_src_28000.user_id - ) subq_43 + WHERE visit__referrer_id = '123456' + ) subq_41 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -144,33 +155,29 @@ FROM ( , 1 AS buys , GENERATE_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_46 + ) subq_44 ON ( - subq_43.user = subq_46.user + subq_41.user = subq_44.user ) AND ( ( - subq_43.ds__day <= subq_46.ds__day + subq_41.ds__day <= subq_44.ds__day ) AND ( - subq_43.ds__day > DATE_SUB(CAST(subq_46.ds__day AS DATETIME), INTERVAL 7 day) + subq_41.ds__day > DATE_SUB(CAST(subq_44.ds__day AS DATETIME), INTERVAL 7 day) ) ) - ) subq_47 - LEFT OUTER JOIN - ***************************.dim_users_latest users_latest_src_28000 - ON - subq_47.user = users_latest_src_28000.user_id + ) subq_45 GROUP BY metric_time__day , user__home_state_latest - ) subq_54 + ) subq_48 ON ( - subq_37.user__home_state_latest = subq_54.user__home_state_latest + subq_34.user__home_state_latest = subq_48.user__home_state_latest ) AND ( - subq_37.metric_time__day = subq_54.metric_time__day + subq_34.metric_time__day = subq_48.metric_time__day ) GROUP BY metric_time__day , user__home_state_latest -) subq_55 +) subq_49 diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_query_filters__plan0.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_query_filters__plan0.sql index 72eadd1d20..0e0400677c 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_query_filters__plan0.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_query_filters__plan0.sql @@ -1,15 +1,15 @@ -- Compute Metrics via Expressions SELECT - subq_27.metric_time__day - , subq_27.user__home_state_latest - , CAST(subq_27.buys AS DOUBLE) / CAST(NULLIF(subq_27.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days + subq_24.metric_time__day + , subq_24.user__home_state_latest + , CAST(subq_24.buys AS DOUBLE) / CAST(NULLIF(subq_24.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_9.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , COALESCE(subq_9.user__home_state_latest, subq_26.user__home_state_latest) AS user__home_state_latest + COALESCE(subq_9.metric_time__day, subq_23.metric_time__day) AS metric_time__day + , COALESCE(subq_9.user__home_state_latest, subq_23.user__home_state_latest) AS user__home_state_latest , MAX(subq_9.visits) AS visits - , MAX(subq_26.buys) AS buys + , MAX(subq_23.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -182,451 +182,443 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_25.metric_time__day - , subq_25.user__home_state_latest - , SUM(subq_25.buys) AS buys + subq_22.metric_time__day + , subq_22.user__home_state_latest + , SUM(subq_22.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'user__home_state_latest', 'metric_time__day'] SELECT - subq_24.metric_time__day - , subq_24.user__home_state_latest - , subq_24.buys + subq_21.metric_time__day + , subq_21.user__home_state_latest + , subq_21.buys FROM ( - -- Join Standard Outputs + -- Find conversions for user within the range of 7 day SELECT - subq_21.metric_time__day AS metric_time__day - , subq_21.user AS user - , subq_21.visit__referrer_id AS visit__referrer_id - , subq_23.home_state_latest AS user__home_state_latest - , subq_21.buys AS buys + subq_20.ds__day + , subq_20.metric_time__day + , subq_20.user + , subq_20.visit__referrer_id + , subq_20.user__home_state_latest + , subq_20.buys + , subq_20.visits FROM ( - -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day', 'user'] - SELECT - subq_20.metric_time__day - , subq_20.user - , subq_20.visit__referrer_id - , subq_20.buys + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_16.visits) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_16.visit__referrer_id) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visit__referrer_id + , FIRST_VALUE(subq_16.user__home_state_latest) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user__home_state_latest + , FIRST_VALUE(subq_16.ds__day) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_16.metric_time__day) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS metric_time__day + , FIRST_VALUE(subq_16.user) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_19.mf_internal_uuid AS mf_internal_uuid + , subq_19.buys AS buys FROM ( - -- Find conversions for user within the range of 7 day + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'user__home_state_latest', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_19.ds__day - , subq_19.metric_time__day - , subq_19.user - , subq_19.visit__referrer_id - , subq_19.user__home_state_latest - , subq_19.buys - , subq_19.visits + subq_15.ds__day + , subq_15.metric_time__day + , subq_15.user + , subq_15.visit__referrer_id + , subq_15.user__home_state_latest + , subq_15.visits FROM ( - -- Dedupe the fanout with mf_internal_uuid in the conversion data set - SELECT DISTINCT - FIRST_VALUE(subq_15.visits) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS visits - , FIRST_VALUE(subq_15.visit__referrer_id) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS visit__referrer_id - , FIRST_VALUE(subq_15.user__home_state_latest) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS user__home_state_latest - , FIRST_VALUE(subq_15.ds__day) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS ds__day - , FIRST_VALUE(subq_15.metric_time__day) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS metric_time__day - , FIRST_VALUE(subq_15.user) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS user - , subq_18.mf_internal_uuid AS mf_internal_uuid - , subq_18.buys AS buys + -- Constrain Output with WHERE + SELECT + subq_14.ds__day + , subq_14.ds__week + , subq_14.ds__month + , subq_14.ds__quarter + , subq_14.ds__year + , subq_14.ds__extract_year + , subq_14.ds__extract_quarter + , subq_14.ds__extract_month + , subq_14.ds__extract_day + , subq_14.ds__extract_dow + , subq_14.ds__extract_doy + , subq_14.visit__ds__day + , subq_14.visit__ds__week + , subq_14.visit__ds__month + , subq_14.visit__ds__quarter + , subq_14.visit__ds__year + , subq_14.visit__ds__extract_year + , subq_14.visit__ds__extract_quarter + , subq_14.visit__ds__extract_month + , subq_14.visit__ds__extract_day + , subq_14.visit__ds__extract_dow + , subq_14.visit__ds__extract_doy + , subq_14.metric_time__day + , subq_14.metric_time__week + , subq_14.metric_time__month + , subq_14.metric_time__quarter + , subq_14.metric_time__year + , subq_14.metric_time__extract_year + , subq_14.metric_time__extract_quarter + , subq_14.metric_time__extract_month + , subq_14.metric_time__extract_day + , subq_14.metric_time__extract_dow + , subq_14.metric_time__extract_doy + , subq_14.user + , subq_14.session + , subq_14.visit__user + , subq_14.visit__session + , subq_14.referrer_id + , subq_14.visit__referrer_id + , subq_14.user__home_state_latest + , subq_14.visits + , subq_14.visitors FROM ( - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'user__home_state_latest', 'ds__day', 'metric_time__day', 'user'] + -- Join Standard Outputs SELECT - subq_14.ds__day - , subq_14.metric_time__day - , subq_14.user - , subq_14.visit__referrer_id - , subq_14.user__home_state_latest - , subq_14.visits + subq_11.ds__day AS ds__day + , subq_11.ds__week AS ds__week + , subq_11.ds__month AS ds__month + , subq_11.ds__quarter AS ds__quarter + , subq_11.ds__year AS ds__year + , subq_11.ds__extract_year AS ds__extract_year + , subq_11.ds__extract_quarter AS ds__extract_quarter + , subq_11.ds__extract_month AS ds__extract_month + , subq_11.ds__extract_day AS ds__extract_day + , subq_11.ds__extract_dow AS ds__extract_dow + , subq_11.ds__extract_doy AS ds__extract_doy + , subq_11.visit__ds__day AS visit__ds__day + , subq_11.visit__ds__week AS visit__ds__week + , subq_11.visit__ds__month AS visit__ds__month + , subq_11.visit__ds__quarter AS visit__ds__quarter + , subq_11.visit__ds__year AS visit__ds__year + , subq_11.visit__ds__extract_year AS visit__ds__extract_year + , subq_11.visit__ds__extract_quarter AS visit__ds__extract_quarter + , subq_11.visit__ds__extract_month AS visit__ds__extract_month + , subq_11.visit__ds__extract_day AS visit__ds__extract_day + , subq_11.visit__ds__extract_dow AS visit__ds__extract_dow + , subq_11.visit__ds__extract_doy AS visit__ds__extract_doy + , subq_11.metric_time__day AS metric_time__day + , subq_11.metric_time__week AS metric_time__week + , subq_11.metric_time__month AS metric_time__month + , subq_11.metric_time__quarter AS metric_time__quarter + , subq_11.metric_time__year AS metric_time__year + , subq_11.metric_time__extract_year AS metric_time__extract_year + , subq_11.metric_time__extract_quarter AS metric_time__extract_quarter + , subq_11.metric_time__extract_month AS metric_time__extract_month + , subq_11.metric_time__extract_day AS metric_time__extract_day + , subq_11.metric_time__extract_dow AS metric_time__extract_dow + , subq_11.metric_time__extract_doy AS metric_time__extract_doy + , subq_11.user AS user + , subq_11.session AS session + , subq_11.visit__user AS visit__user + , subq_11.visit__session AS visit__session + , subq_11.referrer_id AS referrer_id + , subq_11.visit__referrer_id AS visit__referrer_id + , subq_13.home_state_latest AS user__home_state_latest + , subq_11.visits AS visits + , subq_11.visitors AS visitors FROM ( - -- Join Standard Outputs + -- Metric Time Dimension 'ds' SELECT - subq_11.ds__day AS ds__day - , subq_11.ds__week AS ds__week - , subq_11.ds__month AS ds__month - , subq_11.ds__quarter AS ds__quarter - , subq_11.ds__year AS ds__year - , subq_11.ds__extract_year AS ds__extract_year - , subq_11.ds__extract_quarter AS ds__extract_quarter - , subq_11.ds__extract_month AS ds__extract_month - , subq_11.ds__extract_day AS ds__extract_day - , subq_11.ds__extract_dow AS ds__extract_dow - , subq_11.ds__extract_doy AS ds__extract_doy - , subq_11.visit__ds__day AS visit__ds__day - , subq_11.visit__ds__week AS visit__ds__week - , subq_11.visit__ds__month AS visit__ds__month - , subq_11.visit__ds__quarter AS visit__ds__quarter - , subq_11.visit__ds__year AS visit__ds__year - , subq_11.visit__ds__extract_year AS visit__ds__extract_year - , subq_11.visit__ds__extract_quarter AS visit__ds__extract_quarter - , subq_11.visit__ds__extract_month AS visit__ds__extract_month - , subq_11.visit__ds__extract_day AS visit__ds__extract_day - , subq_11.visit__ds__extract_dow AS visit__ds__extract_dow - , subq_11.visit__ds__extract_doy AS visit__ds__extract_doy - , subq_11.metric_time__day AS metric_time__day - , subq_11.metric_time__week AS metric_time__week - , subq_11.metric_time__month AS metric_time__month - , subq_11.metric_time__quarter AS metric_time__quarter - , subq_11.metric_time__year AS metric_time__year - , subq_11.metric_time__extract_year AS metric_time__extract_year - , subq_11.metric_time__extract_quarter AS metric_time__extract_quarter - , subq_11.metric_time__extract_month AS metric_time__extract_month - , subq_11.metric_time__extract_day AS metric_time__extract_day - , subq_11.metric_time__extract_dow AS metric_time__extract_dow - , subq_11.metric_time__extract_doy AS metric_time__extract_doy - , subq_11.user AS user - , subq_11.session AS session - , subq_11.visit__user AS visit__user - , subq_11.visit__session AS visit__session - , subq_11.referrer_id AS referrer_id - , subq_11.visit__referrer_id AS visit__referrer_id - , subq_13.home_state_latest AS user__home_state_latest - , subq_11.visits AS visits - , subq_11.visitors AS visitors + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.visit__ds__day + , subq_10.visit__ds__week + , subq_10.visit__ds__month + , subq_10.visit__ds__quarter + , subq_10.visit__ds__year + , subq_10.visit__ds__extract_year + , subq_10.visit__ds__extract_quarter + , subq_10.visit__ds__extract_month + , subq_10.visit__ds__extract_day + , subq_10.visit__ds__extract_dow + , subq_10.visit__ds__extract_doy + , subq_10.ds__day AS metric_time__day + , subq_10.ds__week AS metric_time__week + , subq_10.ds__month AS metric_time__month + , subq_10.ds__quarter AS metric_time__quarter + , subq_10.ds__year AS metric_time__year + , subq_10.ds__extract_year AS metric_time__extract_year + , subq_10.ds__extract_quarter AS metric_time__extract_quarter + , subq_10.ds__extract_month AS metric_time__extract_month + , subq_10.ds__extract_day AS metric_time__extract_day + , subq_10.ds__extract_dow AS metric_time__extract_dow + , subq_10.ds__extract_doy AS metric_time__extract_doy + , subq_10.user + , subq_10.session + , subq_10.visit__user + , subq_10.visit__session + , subq_10.referrer_id + , subq_10.visit__referrer_id + , subq_10.visits + , subq_10.visitors FROM ( - -- Metric Time Dimension 'ds' - SELECT - subq_10.ds__day - , subq_10.ds__week - , subq_10.ds__month - , subq_10.ds__quarter - , subq_10.ds__year - , subq_10.ds__extract_year - , subq_10.ds__extract_quarter - , subq_10.ds__extract_month - , subq_10.ds__extract_day - , subq_10.ds__extract_dow - , subq_10.ds__extract_doy - , subq_10.visit__ds__day - , subq_10.visit__ds__week - , subq_10.visit__ds__month - , subq_10.visit__ds__quarter - , subq_10.visit__ds__year - , subq_10.visit__ds__extract_year - , subq_10.visit__ds__extract_quarter - , subq_10.visit__ds__extract_month - , subq_10.visit__ds__extract_day - , subq_10.visit__ds__extract_dow - , subq_10.visit__ds__extract_doy - , subq_10.ds__day AS metric_time__day - , subq_10.ds__week AS metric_time__week - , subq_10.ds__month AS metric_time__month - , subq_10.ds__quarter AS metric_time__quarter - , subq_10.ds__year AS metric_time__year - , subq_10.ds__extract_year AS metric_time__extract_year - , subq_10.ds__extract_quarter AS metric_time__extract_quarter - , subq_10.ds__extract_month AS metric_time__extract_month - , subq_10.ds__extract_day AS metric_time__extract_day - , subq_10.ds__extract_dow AS metric_time__extract_dow - , subq_10.ds__extract_doy AS metric_time__extract_doy - , subq_10.user - , subq_10.session - , subq_10.visit__user - , subq_10.visit__session - , subq_10.referrer_id - , subq_10.visit__referrer_id - , subq_10.visits - , subq_10.visitors - FROM ( - -- Read Elements From Semantic Model 'visits_source' - SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_10 - ) subq_11 - LEFT OUTER JOIN ( - -- Pass Only Elements: ['home_state_latest', 'user'] + -- Read Elements From Semantic Model 'visits_source' SELECT - subq_12.user - , subq_12.home_state_latest - FROM ( - -- Read Elements From Semantic Model 'users_latest' - SELECT - DATE_TRUNC('day', users_latest_src_28000.ds) AS ds_latest__day - , DATE_TRUNC('week', users_latest_src_28000.ds) AS ds_latest__week - , DATE_TRUNC('month', users_latest_src_28000.ds) AS ds_latest__month - , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS ds_latest__quarter - , DATE_TRUNC('year', users_latest_src_28000.ds) AS ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS ds_latest__extract_day - , EXTRACT(DAYOFWEEK_ISO FROM users_latest_src_28000.ds) AS ds_latest__extract_dow - , EXTRACT(doy FROM users_latest_src_28000.ds) AS ds_latest__extract_doy - , users_latest_src_28000.home_state_latest - , DATE_TRUNC('day', users_latest_src_28000.ds) AS user__ds_latest__day - , DATE_TRUNC('week', users_latest_src_28000.ds) AS user__ds_latest__week - , DATE_TRUNC('month', users_latest_src_28000.ds) AS user__ds_latest__month - , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS user__ds_latest__quarter - , DATE_TRUNC('year', users_latest_src_28000.ds) AS user__ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS user__ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS user__ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS user__ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS user__ds_latest__extract_day - , EXTRACT(DAYOFWEEK_ISO FROM users_latest_src_28000.ds) AS user__ds_latest__extract_dow - , EXTRACT(doy FROM users_latest_src_28000.ds) AS user__ds_latest__extract_doy - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , users_latest_src_28000.user_id AS user - FROM ***************************.dim_users_latest users_latest_src_28000 - ) subq_12 - ) subq_13 - ON - subq_11.user = subq_13.user - ) subq_14 - ) subq_15 - INNER JOIN ( - -- Add column with generated UUID - SELECT - subq_17.ds__day - , subq_17.ds__week - , subq_17.ds__month - , subq_17.ds__quarter - , subq_17.ds__year - , subq_17.ds__extract_year - , subq_17.ds__extract_quarter - , subq_17.ds__extract_month - , subq_17.ds__extract_day - , subq_17.ds__extract_dow - , subq_17.ds__extract_doy - , subq_17.buy__ds__day - , subq_17.buy__ds__week - , subq_17.buy__ds__month - , subq_17.buy__ds__quarter - , subq_17.buy__ds__year - , subq_17.buy__ds__extract_year - , subq_17.buy__ds__extract_quarter - , subq_17.buy__ds__extract_month - , subq_17.buy__ds__extract_day - , subq_17.buy__ds__extract_dow - , subq_17.buy__ds__extract_doy - , subq_17.metric_time__day - , subq_17.metric_time__week - , subq_17.metric_time__month - , subq_17.metric_time__quarter - , subq_17.metric_time__year - , subq_17.metric_time__extract_year - , subq_17.metric_time__extract_quarter - , subq_17.metric_time__extract_month - , subq_17.metric_time__extract_day - , subq_17.metric_time__extract_dow - , subq_17.metric_time__extract_doy - , subq_17.user - , subq_17.session_id - , subq_17.buy__user - , subq_17.buy__session_id - , subq_17.buys - , subq_17.buyers - , UUID() AS mf_internal_uuid - FROM ( - -- Metric Time Dimension 'ds' + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_10 + ) subq_11 + LEFT OUTER JOIN ( + -- Pass Only Elements: ['home_state_latest', 'user'] SELECT - subq_16.ds__day - , subq_16.ds__week - , subq_16.ds__month - , subq_16.ds__quarter - , subq_16.ds__year - , subq_16.ds__extract_year - , subq_16.ds__extract_quarter - , subq_16.ds__extract_month - , subq_16.ds__extract_day - , subq_16.ds__extract_dow - , subq_16.ds__extract_doy - , subq_16.buy__ds__day - , subq_16.buy__ds__week - , subq_16.buy__ds__month - , subq_16.buy__ds__quarter - , subq_16.buy__ds__year - , subq_16.buy__ds__extract_year - , subq_16.buy__ds__extract_quarter - , subq_16.buy__ds__extract_month - , subq_16.buy__ds__extract_day - , subq_16.buy__ds__extract_dow - , subq_16.buy__ds__extract_doy - , subq_16.ds__day AS metric_time__day - , subq_16.ds__week AS metric_time__week - , subq_16.ds__month AS metric_time__month - , subq_16.ds__quarter AS metric_time__quarter - , subq_16.ds__year AS metric_time__year - , subq_16.ds__extract_year AS metric_time__extract_year - , subq_16.ds__extract_quarter AS metric_time__extract_quarter - , subq_16.ds__extract_month AS metric_time__extract_month - , subq_16.ds__extract_day AS metric_time__extract_day - , subq_16.ds__extract_dow AS metric_time__extract_dow - , subq_16.ds__extract_doy AS metric_time__extract_doy - , subq_16.user - , subq_16.session_id - , subq_16.buy__user - , subq_16.buy__session_id - , subq_16.buys - , subq_16.buyers + subq_12.user + , subq_12.home_state_latest FROM ( - -- Read Elements From Semantic Model 'buys_source' + -- Read Elements From Semantic Model 'users_latest' SELECT - 1 AS buys - , buys_source_src_28000.user_id AS buyers - , DATE_TRUNC('day', buys_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', buys_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', buys_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', buys_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day - , EXTRACT(DAYOFWEEK_ISO FROM buys_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM buys_source_src_28000.ds) AS ds__extract_doy - , DATE_TRUNC('day', buys_source_src_28000.ds) AS buy__ds__day - , DATE_TRUNC('week', buys_source_src_28000.ds) AS buy__ds__week - , DATE_TRUNC('month', buys_source_src_28000.ds) AS buy__ds__month - , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS buy__ds__quarter - , DATE_TRUNC('year', buys_source_src_28000.ds) AS buy__ds__year - , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year - , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter - , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month - , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day - , EXTRACT(DAYOFWEEK_ISO FROM buys_source_src_28000.ds) AS buy__ds__extract_dow - , EXTRACT(doy FROM buys_source_src_28000.ds) AS buy__ds__extract_doy - , buys_source_src_28000.user_id AS user - , buys_source_src_28000.session_id - , buys_source_src_28000.user_id AS buy__user - , buys_source_src_28000.session_id AS buy__session_id - FROM ***************************.fct_buys buys_source_src_28000 - ) subq_16 - ) subq_17 - ) subq_18 - ON - ( - subq_15.user = subq_18.user - ) AND ( - ( - subq_15.ds__day <= subq_18.ds__day - ) AND ( - subq_15.ds__day > DATEADD(day, -7, subq_18.ds__day) - ) - ) - ) subq_19 - ) subq_20 - ) subq_21 - LEFT OUTER JOIN ( - -- Pass Only Elements: ['home_state_latest', 'user'] - SELECT - subq_22.user - , subq_22.home_state_latest - FROM ( - -- Read Elements From Semantic Model 'users_latest' + DATE_TRUNC('day', users_latest_src_28000.ds) AS ds_latest__day + , DATE_TRUNC('week', users_latest_src_28000.ds) AS ds_latest__week + , DATE_TRUNC('month', users_latest_src_28000.ds) AS ds_latest__month + , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS ds_latest__quarter + , DATE_TRUNC('year', users_latest_src_28000.ds) AS ds_latest__year + , EXTRACT(year FROM users_latest_src_28000.ds) AS ds_latest__extract_year + , EXTRACT(quarter FROM users_latest_src_28000.ds) AS ds_latest__extract_quarter + , EXTRACT(month FROM users_latest_src_28000.ds) AS ds_latest__extract_month + , EXTRACT(day FROM users_latest_src_28000.ds) AS ds_latest__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM users_latest_src_28000.ds) AS ds_latest__extract_dow + , EXTRACT(doy FROM users_latest_src_28000.ds) AS ds_latest__extract_doy + , users_latest_src_28000.home_state_latest + , DATE_TRUNC('day', users_latest_src_28000.ds) AS user__ds_latest__day + , DATE_TRUNC('week', users_latest_src_28000.ds) AS user__ds_latest__week + , DATE_TRUNC('month', users_latest_src_28000.ds) AS user__ds_latest__month + , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS user__ds_latest__quarter + , DATE_TRUNC('year', users_latest_src_28000.ds) AS user__ds_latest__year + , EXTRACT(year FROM users_latest_src_28000.ds) AS user__ds_latest__extract_year + , EXTRACT(quarter FROM users_latest_src_28000.ds) AS user__ds_latest__extract_quarter + , EXTRACT(month FROM users_latest_src_28000.ds) AS user__ds_latest__extract_month + , EXTRACT(day FROM users_latest_src_28000.ds) AS user__ds_latest__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM users_latest_src_28000.ds) AS user__ds_latest__extract_dow + , EXTRACT(doy FROM users_latest_src_28000.ds) AS user__ds_latest__extract_doy + , users_latest_src_28000.home_state_latest AS user__home_state_latest + , users_latest_src_28000.user_id AS user + FROM ***************************.dim_users_latest users_latest_src_28000 + ) subq_12 + ) subq_13 + ON + subq_11.user = subq_13.user + ) subq_14 + WHERE visit__referrer_id = '123456' + ) subq_15 + ) subq_16 + INNER JOIN ( + -- Add column with generated UUID SELECT - DATE_TRUNC('day', users_latest_src_28000.ds) AS ds_latest__day - , DATE_TRUNC('week', users_latest_src_28000.ds) AS ds_latest__week - , DATE_TRUNC('month', users_latest_src_28000.ds) AS ds_latest__month - , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS ds_latest__quarter - , DATE_TRUNC('year', users_latest_src_28000.ds) AS ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS ds_latest__extract_day - , EXTRACT(DAYOFWEEK_ISO FROM users_latest_src_28000.ds) AS ds_latest__extract_dow - , EXTRACT(doy FROM users_latest_src_28000.ds) AS ds_latest__extract_doy - , users_latest_src_28000.home_state_latest - , DATE_TRUNC('day', users_latest_src_28000.ds) AS user__ds_latest__day - , DATE_TRUNC('week', users_latest_src_28000.ds) AS user__ds_latest__week - , DATE_TRUNC('month', users_latest_src_28000.ds) AS user__ds_latest__month - , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS user__ds_latest__quarter - , DATE_TRUNC('year', users_latest_src_28000.ds) AS user__ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS user__ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS user__ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS user__ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS user__ds_latest__extract_day - , EXTRACT(DAYOFWEEK_ISO FROM users_latest_src_28000.ds) AS user__ds_latest__extract_dow - , EXTRACT(doy FROM users_latest_src_28000.ds) AS user__ds_latest__extract_doy - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , users_latest_src_28000.user_id AS user - FROM ***************************.dim_users_latest users_latest_src_28000 - ) subq_22 - ) subq_23 - ON - subq_21.user = subq_23.user - ) subq_24 - ) subq_25 + subq_18.ds__day + , subq_18.ds__week + , subq_18.ds__month + , subq_18.ds__quarter + , subq_18.ds__year + , subq_18.ds__extract_year + , subq_18.ds__extract_quarter + , subq_18.ds__extract_month + , subq_18.ds__extract_day + , subq_18.ds__extract_dow + , subq_18.ds__extract_doy + , subq_18.buy__ds__day + , subq_18.buy__ds__week + , subq_18.buy__ds__month + , subq_18.buy__ds__quarter + , subq_18.buy__ds__year + , subq_18.buy__ds__extract_year + , subq_18.buy__ds__extract_quarter + , subq_18.buy__ds__extract_month + , subq_18.buy__ds__extract_day + , subq_18.buy__ds__extract_dow + , subq_18.buy__ds__extract_doy + , subq_18.metric_time__day + , subq_18.metric_time__week + , subq_18.metric_time__month + , subq_18.metric_time__quarter + , subq_18.metric_time__year + , subq_18.metric_time__extract_year + , subq_18.metric_time__extract_quarter + , subq_18.metric_time__extract_month + , subq_18.metric_time__extract_day + , subq_18.metric_time__extract_dow + , subq_18.metric_time__extract_doy + , subq_18.user + , subq_18.session_id + , subq_18.buy__user + , subq_18.buy__session_id + , subq_18.buys + , subq_18.buyers + , UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_17.ds__day + , subq_17.ds__week + , subq_17.ds__month + , subq_17.ds__quarter + , subq_17.ds__year + , subq_17.ds__extract_year + , subq_17.ds__extract_quarter + , subq_17.ds__extract_month + , subq_17.ds__extract_day + , subq_17.ds__extract_dow + , subq_17.ds__extract_doy + , subq_17.buy__ds__day + , subq_17.buy__ds__week + , subq_17.buy__ds__month + , subq_17.buy__ds__quarter + , subq_17.buy__ds__year + , subq_17.buy__ds__extract_year + , subq_17.buy__ds__extract_quarter + , subq_17.buy__ds__extract_month + , subq_17.buy__ds__extract_day + , subq_17.buy__ds__extract_dow + , subq_17.buy__ds__extract_doy + , subq_17.ds__day AS metric_time__day + , subq_17.ds__week AS metric_time__week + , subq_17.ds__month AS metric_time__month + , subq_17.ds__quarter AS metric_time__quarter + , subq_17.ds__year AS metric_time__year + , subq_17.ds__extract_year AS metric_time__extract_year + , subq_17.ds__extract_quarter AS metric_time__extract_quarter + , subq_17.ds__extract_month AS metric_time__extract_month + , subq_17.ds__extract_day AS metric_time__extract_day + , subq_17.ds__extract_dow AS metric_time__extract_dow + , subq_17.ds__extract_doy AS metric_time__extract_doy + , subq_17.user + , subq_17.session_id + , subq_17.buy__user + , subq_17.buy__session_id + , subq_17.buys + , subq_17.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_28000.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM buys_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_28000.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM buys_source_src_28000.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS buy__ds__extract_doy + , buys_source_src_28000.user_id AS user + , buys_source_src_28000.session_id + , buys_source_src_28000.user_id AS buy__user + , buys_source_src_28000.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_17 + ) subq_18 + ) subq_19 + ON + ( + subq_16.user = subq_19.user + ) AND ( + ( + subq_16.ds__day <= subq_19.ds__day + ) AND ( + subq_16.ds__day > DATEADD(day, -7, subq_19.ds__day) + ) + ) + ) subq_20 + ) subq_21 + ) subq_22 GROUP BY - subq_25.metric_time__day - , subq_25.user__home_state_latest - ) subq_26 + subq_22.metric_time__day + , subq_22.user__home_state_latest + ) subq_23 ON ( - subq_9.user__home_state_latest = subq_26.user__home_state_latest + subq_9.user__home_state_latest = subq_23.user__home_state_latest ) AND ( - subq_9.metric_time__day = subq_26.metric_time__day + subq_9.metric_time__day = subq_23.metric_time__day ) GROUP BY - COALESCE(subq_9.metric_time__day, subq_26.metric_time__day) - , COALESCE(subq_9.user__home_state_latest, subq_26.user__home_state_latest) -) subq_27 + COALESCE(subq_9.metric_time__day, subq_23.metric_time__day) + , COALESCE(subq_9.user__home_state_latest, subq_23.user__home_state_latest) +) subq_24 diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_query_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_query_filters__plan0_optimized.sql index fc20b83581..112c9f2e62 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_query_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_conversion_metric_query_filters__plan0_optimized.sql @@ -6,10 +6,10 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_37.metric_time__day, subq_54.metric_time__day) AS metric_time__day - , COALESCE(subq_37.user__home_state_latest, subq_54.user__home_state_latest) AS user__home_state_latest - , MAX(subq_37.visits) AS visits - , MAX(subq_54.buys) AS buys + COALESCE(subq_34.metric_time__day, subq_48.metric_time__day) AS metric_time__day + , COALESCE(subq_34.user__home_state_latest, subq_48.user__home_state_latest) AS user__home_state_latest + , MAX(subq_34.visits) AS visits + , MAX(subq_48.buys) AS buys FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'user__home_state_latest', 'metric_time__day'] @@ -22,10 +22,10 @@ FROM ( -- Join Standard Outputs -- Pass Only Elements: ['visits', 'user__home_state_latest', 'visit__referrer_id', 'metric_time__day'] SELECT - subq_30.metric_time__day AS metric_time__day - , subq_30.visit__referrer_id AS visit__referrer_id + subq_27.metric_time__day AS metric_time__day + , subq_27.visit__referrer_id AS visit__referrer_id , users_latest_src_28000.home_state_latest AS user__home_state_latest - , subq_30.visits AS visits + , subq_27.visits AS visits FROM ( -- Read Elements From Semantic Model 'visits_source' -- Metric Time Dimension 'ds' @@ -36,104 +36,115 @@ FROM ( , referrer_id AS visit__referrer_id , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_30 + ) subq_27 LEFT OUTER JOIN ***************************.dim_users_latest users_latest_src_28000 ON - subq_30.user = users_latest_src_28000.user_id - ) subq_34 + subq_27.user = users_latest_src_28000.user_id + ) subq_31 WHERE visit__referrer_id = '123456' GROUP BY metric_time__day , user__home_state_latest - ) subq_37 + ) subq_34 FULL OUTER JOIN ( - -- Join Standard Outputs + -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'user__home_state_latest', 'metric_time__day'] -- Aggregate Measures SELECT - subq_47.metric_time__day AS metric_time__day - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , SUM(subq_47.buys) AS buys + metric_time__day + , user__home_state_latest + , SUM(buys) AS buys FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_43.visits) OVER ( + FIRST_VALUE(subq_41.visits) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_43.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_41.visit__referrer_id) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_43.user__home_state_latest) OVER ( + , FIRST_VALUE(subq_41.user__home_state_latest) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user__home_state_latest - , FIRST_VALUE(subq_43.ds__day) OVER ( + , FIRST_VALUE(subq_41.ds__day) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_43.metric_time__day) OVER ( + , FIRST_VALUE(subq_41.metric_time__day) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_43.user) OVER ( + , FIRST_VALUE(subq_41.user) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_46.mf_internal_uuid AS mf_internal_uuid - , subq_46.buys AS buys + , subq_44.mf_internal_uuid AS mf_internal_uuid + , subq_44.buys AS buys FROM ( - -- Join Standard Outputs + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'user__home_state_latest', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_39.ds__day AS ds__day - , subq_39.metric_time__day AS metric_time__day - , subq_39.user AS user - , subq_39.visit__referrer_id AS visit__referrer_id - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , subq_39.visits AS visits + ds__day + , metric_time__day + , subq_39.user + , visit__referrer_id + , user__home_state_latest + , visits FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 + subq_36.ds__day AS ds__day + , subq_36.metric_time__day AS metric_time__day + , subq_36.user AS user + , subq_36.visit__referrer_id AS visit__referrer_id + , users_latest_src_28000.home_state_latest AS user__home_state_latest + , subq_36.visits AS visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_36 + LEFT OUTER JOIN + ***************************.dim_users_latest users_latest_src_28000 + ON + subq_36.user = users_latest_src_28000.user_id ) subq_39 - LEFT OUTER JOIN - ***************************.dim_users_latest users_latest_src_28000 - ON - subq_39.user = users_latest_src_28000.user_id - ) subq_43 + WHERE visit__referrer_id = '123456' + ) subq_41 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -144,33 +155,29 @@ FROM ( , 1 AS buys , UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_46 + ) subq_44 ON ( - subq_43.user = subq_46.user + subq_41.user = subq_44.user ) AND ( ( - subq_43.ds__day <= subq_46.ds__day + subq_41.ds__day <= subq_44.ds__day ) AND ( - subq_43.ds__day > DATEADD(day, -7, subq_46.ds__day) + subq_41.ds__day > DATEADD(day, -7, subq_44.ds__day) ) ) - ) subq_47 - LEFT OUTER JOIN - ***************************.dim_users_latest users_latest_src_28000 - ON - subq_47.user = users_latest_src_28000.user_id + ) subq_45 GROUP BY - subq_47.metric_time__day - , users_latest_src_28000.home_state_latest - ) subq_54 + metric_time__day + , user__home_state_latest + ) subq_48 ON ( - subq_37.user__home_state_latest = subq_54.user__home_state_latest + subq_34.user__home_state_latest = subq_48.user__home_state_latest ) AND ( - subq_37.metric_time__day = subq_54.metric_time__day + subq_34.metric_time__day = subq_48.metric_time__day ) GROUP BY - COALESCE(subq_37.metric_time__day, subq_54.metric_time__day) - , COALESCE(subq_37.user__home_state_latest, subq_54.user__home_state_latest) -) subq_55 + COALESCE(subq_34.metric_time__day, subq_48.metric_time__day) + , COALESCE(subq_34.user__home_state_latest, subq_48.user__home_state_latest) +) subq_49 diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_query_filters__plan0.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_query_filters__plan0.sql index b1a1412d9d..322648ab86 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_query_filters__plan0.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_query_filters__plan0.sql @@ -1,15 +1,15 @@ -- Compute Metrics via Expressions SELECT - subq_27.metric_time__day - , subq_27.user__home_state_latest - , CAST(subq_27.buys AS DOUBLE) / CAST(NULLIF(subq_27.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days + subq_24.metric_time__day + , subq_24.user__home_state_latest + , CAST(subq_24.buys AS DOUBLE) / CAST(NULLIF(subq_24.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_9.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , COALESCE(subq_9.user__home_state_latest, subq_26.user__home_state_latest) AS user__home_state_latest + COALESCE(subq_9.metric_time__day, subq_23.metric_time__day) AS metric_time__day + , COALESCE(subq_9.user__home_state_latest, subq_23.user__home_state_latest) AS user__home_state_latest , MAX(subq_9.visits) AS visits - , MAX(subq_26.buys) AS buys + , MAX(subq_23.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -182,451 +182,443 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_25.metric_time__day - , subq_25.user__home_state_latest - , SUM(subq_25.buys) AS buys + subq_22.metric_time__day + , subq_22.user__home_state_latest + , SUM(subq_22.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'user__home_state_latest', 'metric_time__day'] SELECT - subq_24.metric_time__day - , subq_24.user__home_state_latest - , subq_24.buys + subq_21.metric_time__day + , subq_21.user__home_state_latest + , subq_21.buys FROM ( - -- Join Standard Outputs + -- Find conversions for user within the range of 7 day SELECT - subq_21.metric_time__day AS metric_time__day - , subq_21.user AS user - , subq_21.visit__referrer_id AS visit__referrer_id - , subq_23.home_state_latest AS user__home_state_latest - , subq_21.buys AS buys + subq_20.ds__day + , subq_20.metric_time__day + , subq_20.user + , subq_20.visit__referrer_id + , subq_20.user__home_state_latest + , subq_20.buys + , subq_20.visits FROM ( - -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day', 'user'] - SELECT - subq_20.metric_time__day - , subq_20.user - , subq_20.visit__referrer_id - , subq_20.buys + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_16.visits) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_16.visit__referrer_id) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visit__referrer_id + , FIRST_VALUE(subq_16.user__home_state_latest) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user__home_state_latest + , FIRST_VALUE(subq_16.ds__day) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_16.metric_time__day) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS metric_time__day + , FIRST_VALUE(subq_16.user) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_19.mf_internal_uuid AS mf_internal_uuid + , subq_19.buys AS buys FROM ( - -- Find conversions for user within the range of 7 day + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'user__home_state_latest', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_19.ds__day - , subq_19.metric_time__day - , subq_19.user - , subq_19.visit__referrer_id - , subq_19.user__home_state_latest - , subq_19.buys - , subq_19.visits + subq_15.ds__day + , subq_15.metric_time__day + , subq_15.user + , subq_15.visit__referrer_id + , subq_15.user__home_state_latest + , subq_15.visits FROM ( - -- Dedupe the fanout with mf_internal_uuid in the conversion data set - SELECT DISTINCT - FIRST_VALUE(subq_15.visits) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS visits - , FIRST_VALUE(subq_15.visit__referrer_id) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS visit__referrer_id - , FIRST_VALUE(subq_15.user__home_state_latest) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS user__home_state_latest - , FIRST_VALUE(subq_15.ds__day) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS ds__day - , FIRST_VALUE(subq_15.metric_time__day) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS metric_time__day - , FIRST_VALUE(subq_15.user) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS user - , subq_18.mf_internal_uuid AS mf_internal_uuid - , subq_18.buys AS buys + -- Constrain Output with WHERE + SELECT + subq_14.ds__day + , subq_14.ds__week + , subq_14.ds__month + , subq_14.ds__quarter + , subq_14.ds__year + , subq_14.ds__extract_year + , subq_14.ds__extract_quarter + , subq_14.ds__extract_month + , subq_14.ds__extract_day + , subq_14.ds__extract_dow + , subq_14.ds__extract_doy + , subq_14.visit__ds__day + , subq_14.visit__ds__week + , subq_14.visit__ds__month + , subq_14.visit__ds__quarter + , subq_14.visit__ds__year + , subq_14.visit__ds__extract_year + , subq_14.visit__ds__extract_quarter + , subq_14.visit__ds__extract_month + , subq_14.visit__ds__extract_day + , subq_14.visit__ds__extract_dow + , subq_14.visit__ds__extract_doy + , subq_14.metric_time__day + , subq_14.metric_time__week + , subq_14.metric_time__month + , subq_14.metric_time__quarter + , subq_14.metric_time__year + , subq_14.metric_time__extract_year + , subq_14.metric_time__extract_quarter + , subq_14.metric_time__extract_month + , subq_14.metric_time__extract_day + , subq_14.metric_time__extract_dow + , subq_14.metric_time__extract_doy + , subq_14.user + , subq_14.session + , subq_14.visit__user + , subq_14.visit__session + , subq_14.referrer_id + , subq_14.visit__referrer_id + , subq_14.user__home_state_latest + , subq_14.visits + , subq_14.visitors FROM ( - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'user__home_state_latest', 'ds__day', 'metric_time__day', 'user'] + -- Join Standard Outputs SELECT - subq_14.ds__day - , subq_14.metric_time__day - , subq_14.user - , subq_14.visit__referrer_id - , subq_14.user__home_state_latest - , subq_14.visits + subq_11.ds__day AS ds__day + , subq_11.ds__week AS ds__week + , subq_11.ds__month AS ds__month + , subq_11.ds__quarter AS ds__quarter + , subq_11.ds__year AS ds__year + , subq_11.ds__extract_year AS ds__extract_year + , subq_11.ds__extract_quarter AS ds__extract_quarter + , subq_11.ds__extract_month AS ds__extract_month + , subq_11.ds__extract_day AS ds__extract_day + , subq_11.ds__extract_dow AS ds__extract_dow + , subq_11.ds__extract_doy AS ds__extract_doy + , subq_11.visit__ds__day AS visit__ds__day + , subq_11.visit__ds__week AS visit__ds__week + , subq_11.visit__ds__month AS visit__ds__month + , subq_11.visit__ds__quarter AS visit__ds__quarter + , subq_11.visit__ds__year AS visit__ds__year + , subq_11.visit__ds__extract_year AS visit__ds__extract_year + , subq_11.visit__ds__extract_quarter AS visit__ds__extract_quarter + , subq_11.visit__ds__extract_month AS visit__ds__extract_month + , subq_11.visit__ds__extract_day AS visit__ds__extract_day + , subq_11.visit__ds__extract_dow AS visit__ds__extract_dow + , subq_11.visit__ds__extract_doy AS visit__ds__extract_doy + , subq_11.metric_time__day AS metric_time__day + , subq_11.metric_time__week AS metric_time__week + , subq_11.metric_time__month AS metric_time__month + , subq_11.metric_time__quarter AS metric_time__quarter + , subq_11.metric_time__year AS metric_time__year + , subq_11.metric_time__extract_year AS metric_time__extract_year + , subq_11.metric_time__extract_quarter AS metric_time__extract_quarter + , subq_11.metric_time__extract_month AS metric_time__extract_month + , subq_11.metric_time__extract_day AS metric_time__extract_day + , subq_11.metric_time__extract_dow AS metric_time__extract_dow + , subq_11.metric_time__extract_doy AS metric_time__extract_doy + , subq_11.user AS user + , subq_11.session AS session + , subq_11.visit__user AS visit__user + , subq_11.visit__session AS visit__session + , subq_11.referrer_id AS referrer_id + , subq_11.visit__referrer_id AS visit__referrer_id + , subq_13.home_state_latest AS user__home_state_latest + , subq_11.visits AS visits + , subq_11.visitors AS visitors FROM ( - -- Join Standard Outputs + -- Metric Time Dimension 'ds' SELECT - subq_11.ds__day AS ds__day - , subq_11.ds__week AS ds__week - , subq_11.ds__month AS ds__month - , subq_11.ds__quarter AS ds__quarter - , subq_11.ds__year AS ds__year - , subq_11.ds__extract_year AS ds__extract_year - , subq_11.ds__extract_quarter AS ds__extract_quarter - , subq_11.ds__extract_month AS ds__extract_month - , subq_11.ds__extract_day AS ds__extract_day - , subq_11.ds__extract_dow AS ds__extract_dow - , subq_11.ds__extract_doy AS ds__extract_doy - , subq_11.visit__ds__day AS visit__ds__day - , subq_11.visit__ds__week AS visit__ds__week - , subq_11.visit__ds__month AS visit__ds__month - , subq_11.visit__ds__quarter AS visit__ds__quarter - , subq_11.visit__ds__year AS visit__ds__year - , subq_11.visit__ds__extract_year AS visit__ds__extract_year - , subq_11.visit__ds__extract_quarter AS visit__ds__extract_quarter - , subq_11.visit__ds__extract_month AS visit__ds__extract_month - , subq_11.visit__ds__extract_day AS visit__ds__extract_day - , subq_11.visit__ds__extract_dow AS visit__ds__extract_dow - , subq_11.visit__ds__extract_doy AS visit__ds__extract_doy - , subq_11.metric_time__day AS metric_time__day - , subq_11.metric_time__week AS metric_time__week - , subq_11.metric_time__month AS metric_time__month - , subq_11.metric_time__quarter AS metric_time__quarter - , subq_11.metric_time__year AS metric_time__year - , subq_11.metric_time__extract_year AS metric_time__extract_year - , subq_11.metric_time__extract_quarter AS metric_time__extract_quarter - , subq_11.metric_time__extract_month AS metric_time__extract_month - , subq_11.metric_time__extract_day AS metric_time__extract_day - , subq_11.metric_time__extract_dow AS metric_time__extract_dow - , subq_11.metric_time__extract_doy AS metric_time__extract_doy - , subq_11.user AS user - , subq_11.session AS session - , subq_11.visit__user AS visit__user - , subq_11.visit__session AS visit__session - , subq_11.referrer_id AS referrer_id - , subq_11.visit__referrer_id AS visit__referrer_id - , subq_13.home_state_latest AS user__home_state_latest - , subq_11.visits AS visits - , subq_11.visitors AS visitors + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.visit__ds__day + , subq_10.visit__ds__week + , subq_10.visit__ds__month + , subq_10.visit__ds__quarter + , subq_10.visit__ds__year + , subq_10.visit__ds__extract_year + , subq_10.visit__ds__extract_quarter + , subq_10.visit__ds__extract_month + , subq_10.visit__ds__extract_day + , subq_10.visit__ds__extract_dow + , subq_10.visit__ds__extract_doy + , subq_10.ds__day AS metric_time__day + , subq_10.ds__week AS metric_time__week + , subq_10.ds__month AS metric_time__month + , subq_10.ds__quarter AS metric_time__quarter + , subq_10.ds__year AS metric_time__year + , subq_10.ds__extract_year AS metric_time__extract_year + , subq_10.ds__extract_quarter AS metric_time__extract_quarter + , subq_10.ds__extract_month AS metric_time__extract_month + , subq_10.ds__extract_day AS metric_time__extract_day + , subq_10.ds__extract_dow AS metric_time__extract_dow + , subq_10.ds__extract_doy AS metric_time__extract_doy + , subq_10.user + , subq_10.session + , subq_10.visit__user + , subq_10.visit__session + , subq_10.referrer_id + , subq_10.visit__referrer_id + , subq_10.visits + , subq_10.visitors FROM ( - -- Metric Time Dimension 'ds' - SELECT - subq_10.ds__day - , subq_10.ds__week - , subq_10.ds__month - , subq_10.ds__quarter - , subq_10.ds__year - , subq_10.ds__extract_year - , subq_10.ds__extract_quarter - , subq_10.ds__extract_month - , subq_10.ds__extract_day - , subq_10.ds__extract_dow - , subq_10.ds__extract_doy - , subq_10.visit__ds__day - , subq_10.visit__ds__week - , subq_10.visit__ds__month - , subq_10.visit__ds__quarter - , subq_10.visit__ds__year - , subq_10.visit__ds__extract_year - , subq_10.visit__ds__extract_quarter - , subq_10.visit__ds__extract_month - , subq_10.visit__ds__extract_day - , subq_10.visit__ds__extract_dow - , subq_10.visit__ds__extract_doy - , subq_10.ds__day AS metric_time__day - , subq_10.ds__week AS metric_time__week - , subq_10.ds__month AS metric_time__month - , subq_10.ds__quarter AS metric_time__quarter - , subq_10.ds__year AS metric_time__year - , subq_10.ds__extract_year AS metric_time__extract_year - , subq_10.ds__extract_quarter AS metric_time__extract_quarter - , subq_10.ds__extract_month AS metric_time__extract_month - , subq_10.ds__extract_day AS metric_time__extract_day - , subq_10.ds__extract_dow AS metric_time__extract_dow - , subq_10.ds__extract_doy AS metric_time__extract_doy - , subq_10.user - , subq_10.session - , subq_10.visit__user - , subq_10.visit__session - , subq_10.referrer_id - , subq_10.visit__referrer_id - , subq_10.visits - , subq_10.visitors - FROM ( - -- Read Elements From Semantic Model 'visits_source' - SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_10 - ) subq_11 - LEFT OUTER JOIN ( - -- Pass Only Elements: ['home_state_latest', 'user'] + -- Read Elements From Semantic Model 'visits_source' SELECT - subq_12.user - , subq_12.home_state_latest - FROM ( - -- Read Elements From Semantic Model 'users_latest' - SELECT - DATE_TRUNC('day', users_latest_src_28000.ds) AS ds_latest__day - , DATE_TRUNC('week', users_latest_src_28000.ds) AS ds_latest__week - , DATE_TRUNC('month', users_latest_src_28000.ds) AS ds_latest__month - , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS ds_latest__quarter - , DATE_TRUNC('year', users_latest_src_28000.ds) AS ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS ds_latest__extract_day - , EXTRACT(isodow FROM users_latest_src_28000.ds) AS ds_latest__extract_dow - , EXTRACT(doy FROM users_latest_src_28000.ds) AS ds_latest__extract_doy - , users_latest_src_28000.home_state_latest - , DATE_TRUNC('day', users_latest_src_28000.ds) AS user__ds_latest__day - , DATE_TRUNC('week', users_latest_src_28000.ds) AS user__ds_latest__week - , DATE_TRUNC('month', users_latest_src_28000.ds) AS user__ds_latest__month - , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS user__ds_latest__quarter - , DATE_TRUNC('year', users_latest_src_28000.ds) AS user__ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS user__ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS user__ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS user__ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS user__ds_latest__extract_day - , EXTRACT(isodow FROM users_latest_src_28000.ds) AS user__ds_latest__extract_dow - , EXTRACT(doy FROM users_latest_src_28000.ds) AS user__ds_latest__extract_doy - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , users_latest_src_28000.user_id AS user - FROM ***************************.dim_users_latest users_latest_src_28000 - ) subq_12 - ) subq_13 - ON - subq_11.user = subq_13.user - ) subq_14 - ) subq_15 - INNER JOIN ( - -- Add column with generated UUID - SELECT - subq_17.ds__day - , subq_17.ds__week - , subq_17.ds__month - , subq_17.ds__quarter - , subq_17.ds__year - , subq_17.ds__extract_year - , subq_17.ds__extract_quarter - , subq_17.ds__extract_month - , subq_17.ds__extract_day - , subq_17.ds__extract_dow - , subq_17.ds__extract_doy - , subq_17.buy__ds__day - , subq_17.buy__ds__week - , subq_17.buy__ds__month - , subq_17.buy__ds__quarter - , subq_17.buy__ds__year - , subq_17.buy__ds__extract_year - , subq_17.buy__ds__extract_quarter - , subq_17.buy__ds__extract_month - , subq_17.buy__ds__extract_day - , subq_17.buy__ds__extract_dow - , subq_17.buy__ds__extract_doy - , subq_17.metric_time__day - , subq_17.metric_time__week - , subq_17.metric_time__month - , subq_17.metric_time__quarter - , subq_17.metric_time__year - , subq_17.metric_time__extract_year - , subq_17.metric_time__extract_quarter - , subq_17.metric_time__extract_month - , subq_17.metric_time__extract_day - , subq_17.metric_time__extract_dow - , subq_17.metric_time__extract_doy - , subq_17.user - , subq_17.session_id - , subq_17.buy__user - , subq_17.buy__session_id - , subq_17.buys - , subq_17.buyers - , GEN_RANDOM_UUID() AS mf_internal_uuid - FROM ( - -- Metric Time Dimension 'ds' + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_10 + ) subq_11 + LEFT OUTER JOIN ( + -- Pass Only Elements: ['home_state_latest', 'user'] SELECT - subq_16.ds__day - , subq_16.ds__week - , subq_16.ds__month - , subq_16.ds__quarter - , subq_16.ds__year - , subq_16.ds__extract_year - , subq_16.ds__extract_quarter - , subq_16.ds__extract_month - , subq_16.ds__extract_day - , subq_16.ds__extract_dow - , subq_16.ds__extract_doy - , subq_16.buy__ds__day - , subq_16.buy__ds__week - , subq_16.buy__ds__month - , subq_16.buy__ds__quarter - , subq_16.buy__ds__year - , subq_16.buy__ds__extract_year - , subq_16.buy__ds__extract_quarter - , subq_16.buy__ds__extract_month - , subq_16.buy__ds__extract_day - , subq_16.buy__ds__extract_dow - , subq_16.buy__ds__extract_doy - , subq_16.ds__day AS metric_time__day - , subq_16.ds__week AS metric_time__week - , subq_16.ds__month AS metric_time__month - , subq_16.ds__quarter AS metric_time__quarter - , subq_16.ds__year AS metric_time__year - , subq_16.ds__extract_year AS metric_time__extract_year - , subq_16.ds__extract_quarter AS metric_time__extract_quarter - , subq_16.ds__extract_month AS metric_time__extract_month - , subq_16.ds__extract_day AS metric_time__extract_day - , subq_16.ds__extract_dow AS metric_time__extract_dow - , subq_16.ds__extract_doy AS metric_time__extract_doy - , subq_16.user - , subq_16.session_id - , subq_16.buy__user - , subq_16.buy__session_id - , subq_16.buys - , subq_16.buyers + subq_12.user + , subq_12.home_state_latest FROM ( - -- Read Elements From Semantic Model 'buys_source' + -- Read Elements From Semantic Model 'users_latest' SELECT - 1 AS buys - , buys_source_src_28000.user_id AS buyers - , DATE_TRUNC('day', buys_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', buys_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', buys_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', buys_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day - , EXTRACT(isodow FROM buys_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM buys_source_src_28000.ds) AS ds__extract_doy - , DATE_TRUNC('day', buys_source_src_28000.ds) AS buy__ds__day - , DATE_TRUNC('week', buys_source_src_28000.ds) AS buy__ds__week - , DATE_TRUNC('month', buys_source_src_28000.ds) AS buy__ds__month - , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS buy__ds__quarter - , DATE_TRUNC('year', buys_source_src_28000.ds) AS buy__ds__year - , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year - , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter - , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month - , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day - , EXTRACT(isodow FROM buys_source_src_28000.ds) AS buy__ds__extract_dow - , EXTRACT(doy FROM buys_source_src_28000.ds) AS buy__ds__extract_doy - , buys_source_src_28000.user_id AS user - , buys_source_src_28000.session_id - , buys_source_src_28000.user_id AS buy__user - , buys_source_src_28000.session_id AS buy__session_id - FROM ***************************.fct_buys buys_source_src_28000 - ) subq_16 - ) subq_17 - ) subq_18 - ON - ( - subq_15.user = subq_18.user - ) AND ( - ( - subq_15.ds__day <= subq_18.ds__day - ) AND ( - subq_15.ds__day > subq_18.ds__day - INTERVAL 7 day - ) - ) - ) subq_19 - ) subq_20 - ) subq_21 - LEFT OUTER JOIN ( - -- Pass Only Elements: ['home_state_latest', 'user'] - SELECT - subq_22.user - , subq_22.home_state_latest - FROM ( - -- Read Elements From Semantic Model 'users_latest' + DATE_TRUNC('day', users_latest_src_28000.ds) AS ds_latest__day + , DATE_TRUNC('week', users_latest_src_28000.ds) AS ds_latest__week + , DATE_TRUNC('month', users_latest_src_28000.ds) AS ds_latest__month + , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS ds_latest__quarter + , DATE_TRUNC('year', users_latest_src_28000.ds) AS ds_latest__year + , EXTRACT(year FROM users_latest_src_28000.ds) AS ds_latest__extract_year + , EXTRACT(quarter FROM users_latest_src_28000.ds) AS ds_latest__extract_quarter + , EXTRACT(month FROM users_latest_src_28000.ds) AS ds_latest__extract_month + , EXTRACT(day FROM users_latest_src_28000.ds) AS ds_latest__extract_day + , EXTRACT(isodow FROM users_latest_src_28000.ds) AS ds_latest__extract_dow + , EXTRACT(doy FROM users_latest_src_28000.ds) AS ds_latest__extract_doy + , users_latest_src_28000.home_state_latest + , DATE_TRUNC('day', users_latest_src_28000.ds) AS user__ds_latest__day + , DATE_TRUNC('week', users_latest_src_28000.ds) AS user__ds_latest__week + , DATE_TRUNC('month', users_latest_src_28000.ds) AS user__ds_latest__month + , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS user__ds_latest__quarter + , DATE_TRUNC('year', users_latest_src_28000.ds) AS user__ds_latest__year + , EXTRACT(year FROM users_latest_src_28000.ds) AS user__ds_latest__extract_year + , EXTRACT(quarter FROM users_latest_src_28000.ds) AS user__ds_latest__extract_quarter + , EXTRACT(month FROM users_latest_src_28000.ds) AS user__ds_latest__extract_month + , EXTRACT(day FROM users_latest_src_28000.ds) AS user__ds_latest__extract_day + , EXTRACT(isodow FROM users_latest_src_28000.ds) AS user__ds_latest__extract_dow + , EXTRACT(doy FROM users_latest_src_28000.ds) AS user__ds_latest__extract_doy + , users_latest_src_28000.home_state_latest AS user__home_state_latest + , users_latest_src_28000.user_id AS user + FROM ***************************.dim_users_latest users_latest_src_28000 + ) subq_12 + ) subq_13 + ON + subq_11.user = subq_13.user + ) subq_14 + WHERE visit__referrer_id = '123456' + ) subq_15 + ) subq_16 + INNER JOIN ( + -- Add column with generated UUID SELECT - DATE_TRUNC('day', users_latest_src_28000.ds) AS ds_latest__day - , DATE_TRUNC('week', users_latest_src_28000.ds) AS ds_latest__week - , DATE_TRUNC('month', users_latest_src_28000.ds) AS ds_latest__month - , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS ds_latest__quarter - , DATE_TRUNC('year', users_latest_src_28000.ds) AS ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS ds_latest__extract_day - , EXTRACT(isodow FROM users_latest_src_28000.ds) AS ds_latest__extract_dow - , EXTRACT(doy FROM users_latest_src_28000.ds) AS ds_latest__extract_doy - , users_latest_src_28000.home_state_latest - , DATE_TRUNC('day', users_latest_src_28000.ds) AS user__ds_latest__day - , DATE_TRUNC('week', users_latest_src_28000.ds) AS user__ds_latest__week - , DATE_TRUNC('month', users_latest_src_28000.ds) AS user__ds_latest__month - , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS user__ds_latest__quarter - , DATE_TRUNC('year', users_latest_src_28000.ds) AS user__ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS user__ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS user__ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS user__ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS user__ds_latest__extract_day - , EXTRACT(isodow FROM users_latest_src_28000.ds) AS user__ds_latest__extract_dow - , EXTRACT(doy FROM users_latest_src_28000.ds) AS user__ds_latest__extract_doy - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , users_latest_src_28000.user_id AS user - FROM ***************************.dim_users_latest users_latest_src_28000 - ) subq_22 - ) subq_23 - ON - subq_21.user = subq_23.user - ) subq_24 - ) subq_25 + subq_18.ds__day + , subq_18.ds__week + , subq_18.ds__month + , subq_18.ds__quarter + , subq_18.ds__year + , subq_18.ds__extract_year + , subq_18.ds__extract_quarter + , subq_18.ds__extract_month + , subq_18.ds__extract_day + , subq_18.ds__extract_dow + , subq_18.ds__extract_doy + , subq_18.buy__ds__day + , subq_18.buy__ds__week + , subq_18.buy__ds__month + , subq_18.buy__ds__quarter + , subq_18.buy__ds__year + , subq_18.buy__ds__extract_year + , subq_18.buy__ds__extract_quarter + , subq_18.buy__ds__extract_month + , subq_18.buy__ds__extract_day + , subq_18.buy__ds__extract_dow + , subq_18.buy__ds__extract_doy + , subq_18.metric_time__day + , subq_18.metric_time__week + , subq_18.metric_time__month + , subq_18.metric_time__quarter + , subq_18.metric_time__year + , subq_18.metric_time__extract_year + , subq_18.metric_time__extract_quarter + , subq_18.metric_time__extract_month + , subq_18.metric_time__extract_day + , subq_18.metric_time__extract_dow + , subq_18.metric_time__extract_doy + , subq_18.user + , subq_18.session_id + , subq_18.buy__user + , subq_18.buy__session_id + , subq_18.buys + , subq_18.buyers + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_17.ds__day + , subq_17.ds__week + , subq_17.ds__month + , subq_17.ds__quarter + , subq_17.ds__year + , subq_17.ds__extract_year + , subq_17.ds__extract_quarter + , subq_17.ds__extract_month + , subq_17.ds__extract_day + , subq_17.ds__extract_dow + , subq_17.ds__extract_doy + , subq_17.buy__ds__day + , subq_17.buy__ds__week + , subq_17.buy__ds__month + , subq_17.buy__ds__quarter + , subq_17.buy__ds__year + , subq_17.buy__ds__extract_year + , subq_17.buy__ds__extract_quarter + , subq_17.buy__ds__extract_month + , subq_17.buy__ds__extract_day + , subq_17.buy__ds__extract_dow + , subq_17.buy__ds__extract_doy + , subq_17.ds__day AS metric_time__day + , subq_17.ds__week AS metric_time__week + , subq_17.ds__month AS metric_time__month + , subq_17.ds__quarter AS metric_time__quarter + , subq_17.ds__year AS metric_time__year + , subq_17.ds__extract_year AS metric_time__extract_year + , subq_17.ds__extract_quarter AS metric_time__extract_quarter + , subq_17.ds__extract_month AS metric_time__extract_month + , subq_17.ds__extract_day AS metric_time__extract_day + , subq_17.ds__extract_dow AS metric_time__extract_dow + , subq_17.ds__extract_doy AS metric_time__extract_doy + , subq_17.user + , subq_17.session_id + , subq_17.buy__user + , subq_17.buy__session_id + , subq_17.buys + , subq_17.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_28000.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM buys_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_28000.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day + , EXTRACT(isodow FROM buys_source_src_28000.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS buy__ds__extract_doy + , buys_source_src_28000.user_id AS user + , buys_source_src_28000.session_id + , buys_source_src_28000.user_id AS buy__user + , buys_source_src_28000.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_17 + ) subq_18 + ) subq_19 + ON + ( + subq_16.user = subq_19.user + ) AND ( + ( + subq_16.ds__day <= subq_19.ds__day + ) AND ( + subq_16.ds__day > subq_19.ds__day - INTERVAL 7 day + ) + ) + ) subq_20 + ) subq_21 + ) subq_22 GROUP BY - subq_25.metric_time__day - , subq_25.user__home_state_latest - ) subq_26 + subq_22.metric_time__day + , subq_22.user__home_state_latest + ) subq_23 ON ( - subq_9.user__home_state_latest = subq_26.user__home_state_latest + subq_9.user__home_state_latest = subq_23.user__home_state_latest ) AND ( - subq_9.metric_time__day = subq_26.metric_time__day + subq_9.metric_time__day = subq_23.metric_time__day ) GROUP BY - COALESCE(subq_9.metric_time__day, subq_26.metric_time__day) - , COALESCE(subq_9.user__home_state_latest, subq_26.user__home_state_latest) -) subq_27 + COALESCE(subq_9.metric_time__day, subq_23.metric_time__day) + , COALESCE(subq_9.user__home_state_latest, subq_23.user__home_state_latest) +) subq_24 diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_query_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_query_filters__plan0_optimized.sql index 61e0c5aeec..d79532e5ee 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_query_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_query_filters__plan0_optimized.sql @@ -6,10 +6,10 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_37.metric_time__day, subq_54.metric_time__day) AS metric_time__day - , COALESCE(subq_37.user__home_state_latest, subq_54.user__home_state_latest) AS user__home_state_latest - , MAX(subq_37.visits) AS visits - , MAX(subq_54.buys) AS buys + COALESCE(subq_34.metric_time__day, subq_48.metric_time__day) AS metric_time__day + , COALESCE(subq_34.user__home_state_latest, subq_48.user__home_state_latest) AS user__home_state_latest + , MAX(subq_34.visits) AS visits + , MAX(subq_48.buys) AS buys FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'user__home_state_latest', 'metric_time__day'] @@ -22,10 +22,10 @@ FROM ( -- Join Standard Outputs -- Pass Only Elements: ['visits', 'user__home_state_latest', 'visit__referrer_id', 'metric_time__day'] SELECT - subq_30.metric_time__day AS metric_time__day - , subq_30.visit__referrer_id AS visit__referrer_id + subq_27.metric_time__day AS metric_time__day + , subq_27.visit__referrer_id AS visit__referrer_id , users_latest_src_28000.home_state_latest AS user__home_state_latest - , subq_30.visits AS visits + , subq_27.visits AS visits FROM ( -- Read Elements From Semantic Model 'visits_source' -- Metric Time Dimension 'ds' @@ -36,104 +36,115 @@ FROM ( , referrer_id AS visit__referrer_id , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_30 + ) subq_27 LEFT OUTER JOIN ***************************.dim_users_latest users_latest_src_28000 ON - subq_30.user = users_latest_src_28000.user_id - ) subq_34 + subq_27.user = users_latest_src_28000.user_id + ) subq_31 WHERE visit__referrer_id = '123456' GROUP BY metric_time__day , user__home_state_latest - ) subq_37 + ) subq_34 FULL OUTER JOIN ( - -- Join Standard Outputs + -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'user__home_state_latest', 'metric_time__day'] -- Aggregate Measures SELECT - subq_47.metric_time__day AS metric_time__day - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , SUM(subq_47.buys) AS buys + metric_time__day + , user__home_state_latest + , SUM(buys) AS buys FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_43.visits) OVER ( + FIRST_VALUE(subq_41.visits) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_43.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_41.visit__referrer_id) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_43.user__home_state_latest) OVER ( + , FIRST_VALUE(subq_41.user__home_state_latest) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user__home_state_latest - , FIRST_VALUE(subq_43.ds__day) OVER ( + , FIRST_VALUE(subq_41.ds__day) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_43.metric_time__day) OVER ( + , FIRST_VALUE(subq_41.metric_time__day) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_43.user) OVER ( + , FIRST_VALUE(subq_41.user) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_46.mf_internal_uuid AS mf_internal_uuid - , subq_46.buys AS buys + , subq_44.mf_internal_uuid AS mf_internal_uuid + , subq_44.buys AS buys FROM ( - -- Join Standard Outputs + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'user__home_state_latest', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_39.ds__day AS ds__day - , subq_39.metric_time__day AS metric_time__day - , subq_39.user AS user - , subq_39.visit__referrer_id AS visit__referrer_id - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , subq_39.visits AS visits + ds__day + , metric_time__day + , subq_39.user + , visit__referrer_id + , user__home_state_latest + , visits FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 + subq_36.ds__day AS ds__day + , subq_36.metric_time__day AS metric_time__day + , subq_36.user AS user + , subq_36.visit__referrer_id AS visit__referrer_id + , users_latest_src_28000.home_state_latest AS user__home_state_latest + , subq_36.visits AS visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_36 + LEFT OUTER JOIN + ***************************.dim_users_latest users_latest_src_28000 + ON + subq_36.user = users_latest_src_28000.user_id ) subq_39 - LEFT OUTER JOIN - ***************************.dim_users_latest users_latest_src_28000 - ON - subq_39.user = users_latest_src_28000.user_id - ) subq_43 + WHERE visit__referrer_id = '123456' + ) subq_41 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -144,33 +155,29 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_46 + ) subq_44 ON ( - subq_43.user = subq_46.user + subq_41.user = subq_44.user ) AND ( ( - subq_43.ds__day <= subq_46.ds__day + subq_41.ds__day <= subq_44.ds__day ) AND ( - subq_43.ds__day > subq_46.ds__day - INTERVAL 7 day + subq_41.ds__day > subq_44.ds__day - INTERVAL 7 day ) ) - ) subq_47 - LEFT OUTER JOIN - ***************************.dim_users_latest users_latest_src_28000 - ON - subq_47.user = users_latest_src_28000.user_id + ) subq_45 GROUP BY - subq_47.metric_time__day - , users_latest_src_28000.home_state_latest - ) subq_54 + metric_time__day + , user__home_state_latest + ) subq_48 ON ( - subq_37.user__home_state_latest = subq_54.user__home_state_latest + subq_34.user__home_state_latest = subq_48.user__home_state_latest ) AND ( - subq_37.metric_time__day = subq_54.metric_time__day + subq_34.metric_time__day = subq_48.metric_time__day ) GROUP BY - COALESCE(subq_37.metric_time__day, subq_54.metric_time__day) - , COALESCE(subq_37.user__home_state_latest, subq_54.user__home_state_latest) -) subq_55 + COALESCE(subq_34.metric_time__day, subq_48.metric_time__day) + , COALESCE(subq_34.user__home_state_latest, subq_48.user__home_state_latest) +) subq_49 diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_query_filters__plan0.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_query_filters__plan0.sql index 6204ac7c34..7a62302b1a 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_query_filters__plan0.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_query_filters__plan0.sql @@ -1,15 +1,15 @@ -- Compute Metrics via Expressions SELECT - subq_27.metric_time__day - , subq_27.user__home_state_latest - , CAST(subq_27.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_27.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_7days + subq_24.metric_time__day + , subq_24.user__home_state_latest + , CAST(subq_24.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_24.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_9.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , COALESCE(subq_9.user__home_state_latest, subq_26.user__home_state_latest) AS user__home_state_latest + COALESCE(subq_9.metric_time__day, subq_23.metric_time__day) AS metric_time__day + , COALESCE(subq_9.user__home_state_latest, subq_23.user__home_state_latest) AS user__home_state_latest , MAX(subq_9.visits) AS visits - , MAX(subq_26.buys) AS buys + , MAX(subq_23.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -182,451 +182,443 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_25.metric_time__day - , subq_25.user__home_state_latest - , SUM(subq_25.buys) AS buys + subq_22.metric_time__day + , subq_22.user__home_state_latest + , SUM(subq_22.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'user__home_state_latest', 'metric_time__day'] SELECT - subq_24.metric_time__day - , subq_24.user__home_state_latest - , subq_24.buys + subq_21.metric_time__day + , subq_21.user__home_state_latest + , subq_21.buys FROM ( - -- Join Standard Outputs + -- Find conversions for user within the range of 7 day SELECT - subq_21.metric_time__day AS metric_time__day - , subq_21.user AS user - , subq_21.visit__referrer_id AS visit__referrer_id - , subq_23.home_state_latest AS user__home_state_latest - , subq_21.buys AS buys + subq_20.ds__day + , subq_20.metric_time__day + , subq_20.user + , subq_20.visit__referrer_id + , subq_20.user__home_state_latest + , subq_20.buys + , subq_20.visits FROM ( - -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day', 'user'] - SELECT - subq_20.metric_time__day - , subq_20.user - , subq_20.visit__referrer_id - , subq_20.buys + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_16.visits) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_16.visit__referrer_id) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visit__referrer_id + , FIRST_VALUE(subq_16.user__home_state_latest) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user__home_state_latest + , FIRST_VALUE(subq_16.ds__day) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_16.metric_time__day) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS metric_time__day + , FIRST_VALUE(subq_16.user) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_19.mf_internal_uuid AS mf_internal_uuid + , subq_19.buys AS buys FROM ( - -- Find conversions for user within the range of 7 day + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'user__home_state_latest', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_19.ds__day - , subq_19.metric_time__day - , subq_19.user - , subq_19.visit__referrer_id - , subq_19.user__home_state_latest - , subq_19.buys - , subq_19.visits + subq_15.ds__day + , subq_15.metric_time__day + , subq_15.user + , subq_15.visit__referrer_id + , subq_15.user__home_state_latest + , subq_15.visits FROM ( - -- Dedupe the fanout with mf_internal_uuid in the conversion data set - SELECT DISTINCT - FIRST_VALUE(subq_15.visits) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS visits - , FIRST_VALUE(subq_15.visit__referrer_id) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS visit__referrer_id - , FIRST_VALUE(subq_15.user__home_state_latest) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS user__home_state_latest - , FIRST_VALUE(subq_15.ds__day) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS ds__day - , FIRST_VALUE(subq_15.metric_time__day) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS metric_time__day - , FIRST_VALUE(subq_15.user) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS user - , subq_18.mf_internal_uuid AS mf_internal_uuid - , subq_18.buys AS buys + -- Constrain Output with WHERE + SELECT + subq_14.ds__day + , subq_14.ds__week + , subq_14.ds__month + , subq_14.ds__quarter + , subq_14.ds__year + , subq_14.ds__extract_year + , subq_14.ds__extract_quarter + , subq_14.ds__extract_month + , subq_14.ds__extract_day + , subq_14.ds__extract_dow + , subq_14.ds__extract_doy + , subq_14.visit__ds__day + , subq_14.visit__ds__week + , subq_14.visit__ds__month + , subq_14.visit__ds__quarter + , subq_14.visit__ds__year + , subq_14.visit__ds__extract_year + , subq_14.visit__ds__extract_quarter + , subq_14.visit__ds__extract_month + , subq_14.visit__ds__extract_day + , subq_14.visit__ds__extract_dow + , subq_14.visit__ds__extract_doy + , subq_14.metric_time__day + , subq_14.metric_time__week + , subq_14.metric_time__month + , subq_14.metric_time__quarter + , subq_14.metric_time__year + , subq_14.metric_time__extract_year + , subq_14.metric_time__extract_quarter + , subq_14.metric_time__extract_month + , subq_14.metric_time__extract_day + , subq_14.metric_time__extract_dow + , subq_14.metric_time__extract_doy + , subq_14.user + , subq_14.session + , subq_14.visit__user + , subq_14.visit__session + , subq_14.referrer_id + , subq_14.visit__referrer_id + , subq_14.user__home_state_latest + , subq_14.visits + , subq_14.visitors FROM ( - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'user__home_state_latest', 'ds__day', 'metric_time__day', 'user'] + -- Join Standard Outputs SELECT - subq_14.ds__day - , subq_14.metric_time__day - , subq_14.user - , subq_14.visit__referrer_id - , subq_14.user__home_state_latest - , subq_14.visits + subq_11.ds__day AS ds__day + , subq_11.ds__week AS ds__week + , subq_11.ds__month AS ds__month + , subq_11.ds__quarter AS ds__quarter + , subq_11.ds__year AS ds__year + , subq_11.ds__extract_year AS ds__extract_year + , subq_11.ds__extract_quarter AS ds__extract_quarter + , subq_11.ds__extract_month AS ds__extract_month + , subq_11.ds__extract_day AS ds__extract_day + , subq_11.ds__extract_dow AS ds__extract_dow + , subq_11.ds__extract_doy AS ds__extract_doy + , subq_11.visit__ds__day AS visit__ds__day + , subq_11.visit__ds__week AS visit__ds__week + , subq_11.visit__ds__month AS visit__ds__month + , subq_11.visit__ds__quarter AS visit__ds__quarter + , subq_11.visit__ds__year AS visit__ds__year + , subq_11.visit__ds__extract_year AS visit__ds__extract_year + , subq_11.visit__ds__extract_quarter AS visit__ds__extract_quarter + , subq_11.visit__ds__extract_month AS visit__ds__extract_month + , subq_11.visit__ds__extract_day AS visit__ds__extract_day + , subq_11.visit__ds__extract_dow AS visit__ds__extract_dow + , subq_11.visit__ds__extract_doy AS visit__ds__extract_doy + , subq_11.metric_time__day AS metric_time__day + , subq_11.metric_time__week AS metric_time__week + , subq_11.metric_time__month AS metric_time__month + , subq_11.metric_time__quarter AS metric_time__quarter + , subq_11.metric_time__year AS metric_time__year + , subq_11.metric_time__extract_year AS metric_time__extract_year + , subq_11.metric_time__extract_quarter AS metric_time__extract_quarter + , subq_11.metric_time__extract_month AS metric_time__extract_month + , subq_11.metric_time__extract_day AS metric_time__extract_day + , subq_11.metric_time__extract_dow AS metric_time__extract_dow + , subq_11.metric_time__extract_doy AS metric_time__extract_doy + , subq_11.user AS user + , subq_11.session AS session + , subq_11.visit__user AS visit__user + , subq_11.visit__session AS visit__session + , subq_11.referrer_id AS referrer_id + , subq_11.visit__referrer_id AS visit__referrer_id + , subq_13.home_state_latest AS user__home_state_latest + , subq_11.visits AS visits + , subq_11.visitors AS visitors FROM ( - -- Join Standard Outputs + -- Metric Time Dimension 'ds' SELECT - subq_11.ds__day AS ds__day - , subq_11.ds__week AS ds__week - , subq_11.ds__month AS ds__month - , subq_11.ds__quarter AS ds__quarter - , subq_11.ds__year AS ds__year - , subq_11.ds__extract_year AS ds__extract_year - , subq_11.ds__extract_quarter AS ds__extract_quarter - , subq_11.ds__extract_month AS ds__extract_month - , subq_11.ds__extract_day AS ds__extract_day - , subq_11.ds__extract_dow AS ds__extract_dow - , subq_11.ds__extract_doy AS ds__extract_doy - , subq_11.visit__ds__day AS visit__ds__day - , subq_11.visit__ds__week AS visit__ds__week - , subq_11.visit__ds__month AS visit__ds__month - , subq_11.visit__ds__quarter AS visit__ds__quarter - , subq_11.visit__ds__year AS visit__ds__year - , subq_11.visit__ds__extract_year AS visit__ds__extract_year - , subq_11.visit__ds__extract_quarter AS visit__ds__extract_quarter - , subq_11.visit__ds__extract_month AS visit__ds__extract_month - , subq_11.visit__ds__extract_day AS visit__ds__extract_day - , subq_11.visit__ds__extract_dow AS visit__ds__extract_dow - , subq_11.visit__ds__extract_doy AS visit__ds__extract_doy - , subq_11.metric_time__day AS metric_time__day - , subq_11.metric_time__week AS metric_time__week - , subq_11.metric_time__month AS metric_time__month - , subq_11.metric_time__quarter AS metric_time__quarter - , subq_11.metric_time__year AS metric_time__year - , subq_11.metric_time__extract_year AS metric_time__extract_year - , subq_11.metric_time__extract_quarter AS metric_time__extract_quarter - , subq_11.metric_time__extract_month AS metric_time__extract_month - , subq_11.metric_time__extract_day AS metric_time__extract_day - , subq_11.metric_time__extract_dow AS metric_time__extract_dow - , subq_11.metric_time__extract_doy AS metric_time__extract_doy - , subq_11.user AS user - , subq_11.session AS session - , subq_11.visit__user AS visit__user - , subq_11.visit__session AS visit__session - , subq_11.referrer_id AS referrer_id - , subq_11.visit__referrer_id AS visit__referrer_id - , subq_13.home_state_latest AS user__home_state_latest - , subq_11.visits AS visits - , subq_11.visitors AS visitors + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.visit__ds__day + , subq_10.visit__ds__week + , subq_10.visit__ds__month + , subq_10.visit__ds__quarter + , subq_10.visit__ds__year + , subq_10.visit__ds__extract_year + , subq_10.visit__ds__extract_quarter + , subq_10.visit__ds__extract_month + , subq_10.visit__ds__extract_day + , subq_10.visit__ds__extract_dow + , subq_10.visit__ds__extract_doy + , subq_10.ds__day AS metric_time__day + , subq_10.ds__week AS metric_time__week + , subq_10.ds__month AS metric_time__month + , subq_10.ds__quarter AS metric_time__quarter + , subq_10.ds__year AS metric_time__year + , subq_10.ds__extract_year AS metric_time__extract_year + , subq_10.ds__extract_quarter AS metric_time__extract_quarter + , subq_10.ds__extract_month AS metric_time__extract_month + , subq_10.ds__extract_day AS metric_time__extract_day + , subq_10.ds__extract_dow AS metric_time__extract_dow + , subq_10.ds__extract_doy AS metric_time__extract_doy + , subq_10.user + , subq_10.session + , subq_10.visit__user + , subq_10.visit__session + , subq_10.referrer_id + , subq_10.visit__referrer_id + , subq_10.visits + , subq_10.visitors FROM ( - -- Metric Time Dimension 'ds' - SELECT - subq_10.ds__day - , subq_10.ds__week - , subq_10.ds__month - , subq_10.ds__quarter - , subq_10.ds__year - , subq_10.ds__extract_year - , subq_10.ds__extract_quarter - , subq_10.ds__extract_month - , subq_10.ds__extract_day - , subq_10.ds__extract_dow - , subq_10.ds__extract_doy - , subq_10.visit__ds__day - , subq_10.visit__ds__week - , subq_10.visit__ds__month - , subq_10.visit__ds__quarter - , subq_10.visit__ds__year - , subq_10.visit__ds__extract_year - , subq_10.visit__ds__extract_quarter - , subq_10.visit__ds__extract_month - , subq_10.visit__ds__extract_day - , subq_10.visit__ds__extract_dow - , subq_10.visit__ds__extract_doy - , subq_10.ds__day AS metric_time__day - , subq_10.ds__week AS metric_time__week - , subq_10.ds__month AS metric_time__month - , subq_10.ds__quarter AS metric_time__quarter - , subq_10.ds__year AS metric_time__year - , subq_10.ds__extract_year AS metric_time__extract_year - , subq_10.ds__extract_quarter AS metric_time__extract_quarter - , subq_10.ds__extract_month AS metric_time__extract_month - , subq_10.ds__extract_day AS metric_time__extract_day - , subq_10.ds__extract_dow AS metric_time__extract_dow - , subq_10.ds__extract_doy AS metric_time__extract_doy - , subq_10.user - , subq_10.session - , subq_10.visit__user - , subq_10.visit__session - , subq_10.referrer_id - , subq_10.visit__referrer_id - , subq_10.visits - , subq_10.visitors - FROM ( - -- Read Elements From Semantic Model 'visits_source' - SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_10 - ) subq_11 - LEFT OUTER JOIN ( - -- Pass Only Elements: ['home_state_latest', 'user'] + -- Read Elements From Semantic Model 'visits_source' SELECT - subq_12.user - , subq_12.home_state_latest - FROM ( - -- Read Elements From Semantic Model 'users_latest' - SELECT - DATE_TRUNC('day', users_latest_src_28000.ds) AS ds_latest__day - , DATE_TRUNC('week', users_latest_src_28000.ds) AS ds_latest__week - , DATE_TRUNC('month', users_latest_src_28000.ds) AS ds_latest__month - , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS ds_latest__quarter - , DATE_TRUNC('year', users_latest_src_28000.ds) AS ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS ds_latest__extract_day - , EXTRACT(isodow FROM users_latest_src_28000.ds) AS ds_latest__extract_dow - , EXTRACT(doy FROM users_latest_src_28000.ds) AS ds_latest__extract_doy - , users_latest_src_28000.home_state_latest - , DATE_TRUNC('day', users_latest_src_28000.ds) AS user__ds_latest__day - , DATE_TRUNC('week', users_latest_src_28000.ds) AS user__ds_latest__week - , DATE_TRUNC('month', users_latest_src_28000.ds) AS user__ds_latest__month - , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS user__ds_latest__quarter - , DATE_TRUNC('year', users_latest_src_28000.ds) AS user__ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS user__ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS user__ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS user__ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS user__ds_latest__extract_day - , EXTRACT(isodow FROM users_latest_src_28000.ds) AS user__ds_latest__extract_dow - , EXTRACT(doy FROM users_latest_src_28000.ds) AS user__ds_latest__extract_doy - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , users_latest_src_28000.user_id AS user - FROM ***************************.dim_users_latest users_latest_src_28000 - ) subq_12 - ) subq_13 - ON - subq_11.user = subq_13.user - ) subq_14 - ) subq_15 - INNER JOIN ( - -- Add column with generated UUID - SELECT - subq_17.ds__day - , subq_17.ds__week - , subq_17.ds__month - , subq_17.ds__quarter - , subq_17.ds__year - , subq_17.ds__extract_year - , subq_17.ds__extract_quarter - , subq_17.ds__extract_month - , subq_17.ds__extract_day - , subq_17.ds__extract_dow - , subq_17.ds__extract_doy - , subq_17.buy__ds__day - , subq_17.buy__ds__week - , subq_17.buy__ds__month - , subq_17.buy__ds__quarter - , subq_17.buy__ds__year - , subq_17.buy__ds__extract_year - , subq_17.buy__ds__extract_quarter - , subq_17.buy__ds__extract_month - , subq_17.buy__ds__extract_day - , subq_17.buy__ds__extract_dow - , subq_17.buy__ds__extract_doy - , subq_17.metric_time__day - , subq_17.metric_time__week - , subq_17.metric_time__month - , subq_17.metric_time__quarter - , subq_17.metric_time__year - , subq_17.metric_time__extract_year - , subq_17.metric_time__extract_quarter - , subq_17.metric_time__extract_month - , subq_17.metric_time__extract_day - , subq_17.metric_time__extract_dow - , subq_17.metric_time__extract_doy - , subq_17.user - , subq_17.session_id - , subq_17.buy__user - , subq_17.buy__session_id - , subq_17.buys - , subq_17.buyers - , GEN_RANDOM_UUID() AS mf_internal_uuid - FROM ( - -- Metric Time Dimension 'ds' + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_10 + ) subq_11 + LEFT OUTER JOIN ( + -- Pass Only Elements: ['home_state_latest', 'user'] SELECT - subq_16.ds__day - , subq_16.ds__week - , subq_16.ds__month - , subq_16.ds__quarter - , subq_16.ds__year - , subq_16.ds__extract_year - , subq_16.ds__extract_quarter - , subq_16.ds__extract_month - , subq_16.ds__extract_day - , subq_16.ds__extract_dow - , subq_16.ds__extract_doy - , subq_16.buy__ds__day - , subq_16.buy__ds__week - , subq_16.buy__ds__month - , subq_16.buy__ds__quarter - , subq_16.buy__ds__year - , subq_16.buy__ds__extract_year - , subq_16.buy__ds__extract_quarter - , subq_16.buy__ds__extract_month - , subq_16.buy__ds__extract_day - , subq_16.buy__ds__extract_dow - , subq_16.buy__ds__extract_doy - , subq_16.ds__day AS metric_time__day - , subq_16.ds__week AS metric_time__week - , subq_16.ds__month AS metric_time__month - , subq_16.ds__quarter AS metric_time__quarter - , subq_16.ds__year AS metric_time__year - , subq_16.ds__extract_year AS metric_time__extract_year - , subq_16.ds__extract_quarter AS metric_time__extract_quarter - , subq_16.ds__extract_month AS metric_time__extract_month - , subq_16.ds__extract_day AS metric_time__extract_day - , subq_16.ds__extract_dow AS metric_time__extract_dow - , subq_16.ds__extract_doy AS metric_time__extract_doy - , subq_16.user - , subq_16.session_id - , subq_16.buy__user - , subq_16.buy__session_id - , subq_16.buys - , subq_16.buyers + subq_12.user + , subq_12.home_state_latest FROM ( - -- Read Elements From Semantic Model 'buys_source' + -- Read Elements From Semantic Model 'users_latest' SELECT - 1 AS buys - , buys_source_src_28000.user_id AS buyers - , DATE_TRUNC('day', buys_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', buys_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', buys_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', buys_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day - , EXTRACT(isodow FROM buys_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM buys_source_src_28000.ds) AS ds__extract_doy - , DATE_TRUNC('day', buys_source_src_28000.ds) AS buy__ds__day - , DATE_TRUNC('week', buys_source_src_28000.ds) AS buy__ds__week - , DATE_TRUNC('month', buys_source_src_28000.ds) AS buy__ds__month - , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS buy__ds__quarter - , DATE_TRUNC('year', buys_source_src_28000.ds) AS buy__ds__year - , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year - , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter - , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month - , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day - , EXTRACT(isodow FROM buys_source_src_28000.ds) AS buy__ds__extract_dow - , EXTRACT(doy FROM buys_source_src_28000.ds) AS buy__ds__extract_doy - , buys_source_src_28000.user_id AS user - , buys_source_src_28000.session_id - , buys_source_src_28000.user_id AS buy__user - , buys_source_src_28000.session_id AS buy__session_id - FROM ***************************.fct_buys buys_source_src_28000 - ) subq_16 - ) subq_17 - ) subq_18 - ON - ( - subq_15.user = subq_18.user - ) AND ( - ( - subq_15.ds__day <= subq_18.ds__day - ) AND ( - subq_15.ds__day > subq_18.ds__day - MAKE_INTERVAL(days => 7) - ) - ) - ) subq_19 - ) subq_20 - ) subq_21 - LEFT OUTER JOIN ( - -- Pass Only Elements: ['home_state_latest', 'user'] - SELECT - subq_22.user - , subq_22.home_state_latest - FROM ( - -- Read Elements From Semantic Model 'users_latest' + DATE_TRUNC('day', users_latest_src_28000.ds) AS ds_latest__day + , DATE_TRUNC('week', users_latest_src_28000.ds) AS ds_latest__week + , DATE_TRUNC('month', users_latest_src_28000.ds) AS ds_latest__month + , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS ds_latest__quarter + , DATE_TRUNC('year', users_latest_src_28000.ds) AS ds_latest__year + , EXTRACT(year FROM users_latest_src_28000.ds) AS ds_latest__extract_year + , EXTRACT(quarter FROM users_latest_src_28000.ds) AS ds_latest__extract_quarter + , EXTRACT(month FROM users_latest_src_28000.ds) AS ds_latest__extract_month + , EXTRACT(day FROM users_latest_src_28000.ds) AS ds_latest__extract_day + , EXTRACT(isodow FROM users_latest_src_28000.ds) AS ds_latest__extract_dow + , EXTRACT(doy FROM users_latest_src_28000.ds) AS ds_latest__extract_doy + , users_latest_src_28000.home_state_latest + , DATE_TRUNC('day', users_latest_src_28000.ds) AS user__ds_latest__day + , DATE_TRUNC('week', users_latest_src_28000.ds) AS user__ds_latest__week + , DATE_TRUNC('month', users_latest_src_28000.ds) AS user__ds_latest__month + , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS user__ds_latest__quarter + , DATE_TRUNC('year', users_latest_src_28000.ds) AS user__ds_latest__year + , EXTRACT(year FROM users_latest_src_28000.ds) AS user__ds_latest__extract_year + , EXTRACT(quarter FROM users_latest_src_28000.ds) AS user__ds_latest__extract_quarter + , EXTRACT(month FROM users_latest_src_28000.ds) AS user__ds_latest__extract_month + , EXTRACT(day FROM users_latest_src_28000.ds) AS user__ds_latest__extract_day + , EXTRACT(isodow FROM users_latest_src_28000.ds) AS user__ds_latest__extract_dow + , EXTRACT(doy FROM users_latest_src_28000.ds) AS user__ds_latest__extract_doy + , users_latest_src_28000.home_state_latest AS user__home_state_latest + , users_latest_src_28000.user_id AS user + FROM ***************************.dim_users_latest users_latest_src_28000 + ) subq_12 + ) subq_13 + ON + subq_11.user = subq_13.user + ) subq_14 + WHERE visit__referrer_id = '123456' + ) subq_15 + ) subq_16 + INNER JOIN ( + -- Add column with generated UUID SELECT - DATE_TRUNC('day', users_latest_src_28000.ds) AS ds_latest__day - , DATE_TRUNC('week', users_latest_src_28000.ds) AS ds_latest__week - , DATE_TRUNC('month', users_latest_src_28000.ds) AS ds_latest__month - , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS ds_latest__quarter - , DATE_TRUNC('year', users_latest_src_28000.ds) AS ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS ds_latest__extract_day - , EXTRACT(isodow FROM users_latest_src_28000.ds) AS ds_latest__extract_dow - , EXTRACT(doy FROM users_latest_src_28000.ds) AS ds_latest__extract_doy - , users_latest_src_28000.home_state_latest - , DATE_TRUNC('day', users_latest_src_28000.ds) AS user__ds_latest__day - , DATE_TRUNC('week', users_latest_src_28000.ds) AS user__ds_latest__week - , DATE_TRUNC('month', users_latest_src_28000.ds) AS user__ds_latest__month - , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS user__ds_latest__quarter - , DATE_TRUNC('year', users_latest_src_28000.ds) AS user__ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS user__ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS user__ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS user__ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS user__ds_latest__extract_day - , EXTRACT(isodow FROM users_latest_src_28000.ds) AS user__ds_latest__extract_dow - , EXTRACT(doy FROM users_latest_src_28000.ds) AS user__ds_latest__extract_doy - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , users_latest_src_28000.user_id AS user - FROM ***************************.dim_users_latest users_latest_src_28000 - ) subq_22 - ) subq_23 - ON - subq_21.user = subq_23.user - ) subq_24 - ) subq_25 + subq_18.ds__day + , subq_18.ds__week + , subq_18.ds__month + , subq_18.ds__quarter + , subq_18.ds__year + , subq_18.ds__extract_year + , subq_18.ds__extract_quarter + , subq_18.ds__extract_month + , subq_18.ds__extract_day + , subq_18.ds__extract_dow + , subq_18.ds__extract_doy + , subq_18.buy__ds__day + , subq_18.buy__ds__week + , subq_18.buy__ds__month + , subq_18.buy__ds__quarter + , subq_18.buy__ds__year + , subq_18.buy__ds__extract_year + , subq_18.buy__ds__extract_quarter + , subq_18.buy__ds__extract_month + , subq_18.buy__ds__extract_day + , subq_18.buy__ds__extract_dow + , subq_18.buy__ds__extract_doy + , subq_18.metric_time__day + , subq_18.metric_time__week + , subq_18.metric_time__month + , subq_18.metric_time__quarter + , subq_18.metric_time__year + , subq_18.metric_time__extract_year + , subq_18.metric_time__extract_quarter + , subq_18.metric_time__extract_month + , subq_18.metric_time__extract_day + , subq_18.metric_time__extract_dow + , subq_18.metric_time__extract_doy + , subq_18.user + , subq_18.session_id + , subq_18.buy__user + , subq_18.buy__session_id + , subq_18.buys + , subq_18.buyers + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_17.ds__day + , subq_17.ds__week + , subq_17.ds__month + , subq_17.ds__quarter + , subq_17.ds__year + , subq_17.ds__extract_year + , subq_17.ds__extract_quarter + , subq_17.ds__extract_month + , subq_17.ds__extract_day + , subq_17.ds__extract_dow + , subq_17.ds__extract_doy + , subq_17.buy__ds__day + , subq_17.buy__ds__week + , subq_17.buy__ds__month + , subq_17.buy__ds__quarter + , subq_17.buy__ds__year + , subq_17.buy__ds__extract_year + , subq_17.buy__ds__extract_quarter + , subq_17.buy__ds__extract_month + , subq_17.buy__ds__extract_day + , subq_17.buy__ds__extract_dow + , subq_17.buy__ds__extract_doy + , subq_17.ds__day AS metric_time__day + , subq_17.ds__week AS metric_time__week + , subq_17.ds__month AS metric_time__month + , subq_17.ds__quarter AS metric_time__quarter + , subq_17.ds__year AS metric_time__year + , subq_17.ds__extract_year AS metric_time__extract_year + , subq_17.ds__extract_quarter AS metric_time__extract_quarter + , subq_17.ds__extract_month AS metric_time__extract_month + , subq_17.ds__extract_day AS metric_time__extract_day + , subq_17.ds__extract_dow AS metric_time__extract_dow + , subq_17.ds__extract_doy AS metric_time__extract_doy + , subq_17.user + , subq_17.session_id + , subq_17.buy__user + , subq_17.buy__session_id + , subq_17.buys + , subq_17.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_28000.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM buys_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_28000.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day + , EXTRACT(isodow FROM buys_source_src_28000.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS buy__ds__extract_doy + , buys_source_src_28000.user_id AS user + , buys_source_src_28000.session_id + , buys_source_src_28000.user_id AS buy__user + , buys_source_src_28000.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_17 + ) subq_18 + ) subq_19 + ON + ( + subq_16.user = subq_19.user + ) AND ( + ( + subq_16.ds__day <= subq_19.ds__day + ) AND ( + subq_16.ds__day > subq_19.ds__day - MAKE_INTERVAL(days => 7) + ) + ) + ) subq_20 + ) subq_21 + ) subq_22 GROUP BY - subq_25.metric_time__day - , subq_25.user__home_state_latest - ) subq_26 + subq_22.metric_time__day + , subq_22.user__home_state_latest + ) subq_23 ON ( - subq_9.user__home_state_latest = subq_26.user__home_state_latest + subq_9.user__home_state_latest = subq_23.user__home_state_latest ) AND ( - subq_9.metric_time__day = subq_26.metric_time__day + subq_9.metric_time__day = subq_23.metric_time__day ) GROUP BY - COALESCE(subq_9.metric_time__day, subq_26.metric_time__day) - , COALESCE(subq_9.user__home_state_latest, subq_26.user__home_state_latest) -) subq_27 + COALESCE(subq_9.metric_time__day, subq_23.metric_time__day) + , COALESCE(subq_9.user__home_state_latest, subq_23.user__home_state_latest) +) subq_24 diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_query_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_query_filters__plan0_optimized.sql index 64717e94ef..1e7c9a76a0 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_query_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_conversion_metric_query_filters__plan0_optimized.sql @@ -6,10 +6,10 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_37.metric_time__day, subq_54.metric_time__day) AS metric_time__day - , COALESCE(subq_37.user__home_state_latest, subq_54.user__home_state_latest) AS user__home_state_latest - , MAX(subq_37.visits) AS visits - , MAX(subq_54.buys) AS buys + COALESCE(subq_34.metric_time__day, subq_48.metric_time__day) AS metric_time__day + , COALESCE(subq_34.user__home_state_latest, subq_48.user__home_state_latest) AS user__home_state_latest + , MAX(subq_34.visits) AS visits + , MAX(subq_48.buys) AS buys FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'user__home_state_latest', 'metric_time__day'] @@ -22,10 +22,10 @@ FROM ( -- Join Standard Outputs -- Pass Only Elements: ['visits', 'user__home_state_latest', 'visit__referrer_id', 'metric_time__day'] SELECT - subq_30.metric_time__day AS metric_time__day - , subq_30.visit__referrer_id AS visit__referrer_id + subq_27.metric_time__day AS metric_time__day + , subq_27.visit__referrer_id AS visit__referrer_id , users_latest_src_28000.home_state_latest AS user__home_state_latest - , subq_30.visits AS visits + , subq_27.visits AS visits FROM ( -- Read Elements From Semantic Model 'visits_source' -- Metric Time Dimension 'ds' @@ -36,104 +36,115 @@ FROM ( , referrer_id AS visit__referrer_id , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_30 + ) subq_27 LEFT OUTER JOIN ***************************.dim_users_latest users_latest_src_28000 ON - subq_30.user = users_latest_src_28000.user_id - ) subq_34 + subq_27.user = users_latest_src_28000.user_id + ) subq_31 WHERE visit__referrer_id = '123456' GROUP BY metric_time__day , user__home_state_latest - ) subq_37 + ) subq_34 FULL OUTER JOIN ( - -- Join Standard Outputs + -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'user__home_state_latest', 'metric_time__day'] -- Aggregate Measures SELECT - subq_47.metric_time__day AS metric_time__day - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , SUM(subq_47.buys) AS buys + metric_time__day + , user__home_state_latest + , SUM(buys) AS buys FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_43.visits) OVER ( + FIRST_VALUE(subq_41.visits) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_43.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_41.visit__referrer_id) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_43.user__home_state_latest) OVER ( + , FIRST_VALUE(subq_41.user__home_state_latest) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user__home_state_latest - , FIRST_VALUE(subq_43.ds__day) OVER ( + , FIRST_VALUE(subq_41.ds__day) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_43.metric_time__day) OVER ( + , FIRST_VALUE(subq_41.metric_time__day) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_43.user) OVER ( + , FIRST_VALUE(subq_41.user) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_46.mf_internal_uuid AS mf_internal_uuid - , subq_46.buys AS buys + , subq_44.mf_internal_uuid AS mf_internal_uuid + , subq_44.buys AS buys FROM ( - -- Join Standard Outputs + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'user__home_state_latest', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_39.ds__day AS ds__day - , subq_39.metric_time__day AS metric_time__day - , subq_39.user AS user - , subq_39.visit__referrer_id AS visit__referrer_id - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , subq_39.visits AS visits + ds__day + , metric_time__day + , subq_39.user + , visit__referrer_id + , user__home_state_latest + , visits FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 + subq_36.ds__day AS ds__day + , subq_36.metric_time__day AS metric_time__day + , subq_36.user AS user + , subq_36.visit__referrer_id AS visit__referrer_id + , users_latest_src_28000.home_state_latest AS user__home_state_latest + , subq_36.visits AS visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_36 + LEFT OUTER JOIN + ***************************.dim_users_latest users_latest_src_28000 + ON + subq_36.user = users_latest_src_28000.user_id ) subq_39 - LEFT OUTER JOIN - ***************************.dim_users_latest users_latest_src_28000 - ON - subq_39.user = users_latest_src_28000.user_id - ) subq_43 + WHERE visit__referrer_id = '123456' + ) subq_41 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -144,33 +155,29 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_46 + ) subq_44 ON ( - subq_43.user = subq_46.user + subq_41.user = subq_44.user ) AND ( ( - subq_43.ds__day <= subq_46.ds__day + subq_41.ds__day <= subq_44.ds__day ) AND ( - subq_43.ds__day > subq_46.ds__day - MAKE_INTERVAL(days => 7) + subq_41.ds__day > subq_44.ds__day - MAKE_INTERVAL(days => 7) ) ) - ) subq_47 - LEFT OUTER JOIN - ***************************.dim_users_latest users_latest_src_28000 - ON - subq_47.user = users_latest_src_28000.user_id + ) subq_45 GROUP BY - subq_47.metric_time__day - , users_latest_src_28000.home_state_latest - ) subq_54 + metric_time__day + , user__home_state_latest + ) subq_48 ON ( - subq_37.user__home_state_latest = subq_54.user__home_state_latest + subq_34.user__home_state_latest = subq_48.user__home_state_latest ) AND ( - subq_37.metric_time__day = subq_54.metric_time__day + subq_34.metric_time__day = subq_48.metric_time__day ) GROUP BY - COALESCE(subq_37.metric_time__day, subq_54.metric_time__day) - , COALESCE(subq_37.user__home_state_latest, subq_54.user__home_state_latest) -) subq_55 + COALESCE(subq_34.metric_time__day, subq_48.metric_time__day) + , COALESCE(subq_34.user__home_state_latest, subq_48.user__home_state_latest) +) subq_49 diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_query_filters__plan0.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_query_filters__plan0.sql index eaa5a32f5c..81cf0db56f 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_query_filters__plan0.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_query_filters__plan0.sql @@ -1,15 +1,15 @@ -- Compute Metrics via Expressions SELECT - subq_27.metric_time__day - , subq_27.user__home_state_latest - , CAST(subq_27.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_27.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_7days + subq_24.metric_time__day + , subq_24.user__home_state_latest + , CAST(subq_24.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_24.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_9.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , COALESCE(subq_9.user__home_state_latest, subq_26.user__home_state_latest) AS user__home_state_latest + COALESCE(subq_9.metric_time__day, subq_23.metric_time__day) AS metric_time__day + , COALESCE(subq_9.user__home_state_latest, subq_23.user__home_state_latest) AS user__home_state_latest , MAX(subq_9.visits) AS visits - , MAX(subq_26.buys) AS buys + , MAX(subq_23.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -182,451 +182,443 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_25.metric_time__day - , subq_25.user__home_state_latest - , SUM(subq_25.buys) AS buys + subq_22.metric_time__day + , subq_22.user__home_state_latest + , SUM(subq_22.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'user__home_state_latest', 'metric_time__day'] SELECT - subq_24.metric_time__day - , subq_24.user__home_state_latest - , subq_24.buys + subq_21.metric_time__day + , subq_21.user__home_state_latest + , subq_21.buys FROM ( - -- Join Standard Outputs + -- Find conversions for user within the range of 7 day SELECT - subq_21.metric_time__day AS metric_time__day - , subq_21.user AS user - , subq_21.visit__referrer_id AS visit__referrer_id - , subq_23.home_state_latest AS user__home_state_latest - , subq_21.buys AS buys + subq_20.ds__day + , subq_20.metric_time__day + , subq_20.user + , subq_20.visit__referrer_id + , subq_20.user__home_state_latest + , subq_20.buys + , subq_20.visits FROM ( - -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day', 'user'] - SELECT - subq_20.metric_time__day - , subq_20.user - , subq_20.visit__referrer_id - , subq_20.buys + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_16.visits) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_16.visit__referrer_id) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visit__referrer_id + , FIRST_VALUE(subq_16.user__home_state_latest) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user__home_state_latest + , FIRST_VALUE(subq_16.ds__day) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_16.metric_time__day) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS metric_time__day + , FIRST_VALUE(subq_16.user) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_19.mf_internal_uuid AS mf_internal_uuid + , subq_19.buys AS buys FROM ( - -- Find conversions for user within the range of 7 day + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'user__home_state_latest', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_19.ds__day - , subq_19.metric_time__day - , subq_19.user - , subq_19.visit__referrer_id - , subq_19.user__home_state_latest - , subq_19.buys - , subq_19.visits + subq_15.ds__day + , subq_15.metric_time__day + , subq_15.user + , subq_15.visit__referrer_id + , subq_15.user__home_state_latest + , subq_15.visits FROM ( - -- Dedupe the fanout with mf_internal_uuid in the conversion data set - SELECT DISTINCT - FIRST_VALUE(subq_15.visits) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS visits - , FIRST_VALUE(subq_15.visit__referrer_id) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS visit__referrer_id - , FIRST_VALUE(subq_15.user__home_state_latest) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS user__home_state_latest - , FIRST_VALUE(subq_15.ds__day) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS ds__day - , FIRST_VALUE(subq_15.metric_time__day) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS metric_time__day - , FIRST_VALUE(subq_15.user) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS user - , subq_18.mf_internal_uuid AS mf_internal_uuid - , subq_18.buys AS buys + -- Constrain Output with WHERE + SELECT + subq_14.ds__day + , subq_14.ds__week + , subq_14.ds__month + , subq_14.ds__quarter + , subq_14.ds__year + , subq_14.ds__extract_year + , subq_14.ds__extract_quarter + , subq_14.ds__extract_month + , subq_14.ds__extract_day + , subq_14.ds__extract_dow + , subq_14.ds__extract_doy + , subq_14.visit__ds__day + , subq_14.visit__ds__week + , subq_14.visit__ds__month + , subq_14.visit__ds__quarter + , subq_14.visit__ds__year + , subq_14.visit__ds__extract_year + , subq_14.visit__ds__extract_quarter + , subq_14.visit__ds__extract_month + , subq_14.visit__ds__extract_day + , subq_14.visit__ds__extract_dow + , subq_14.visit__ds__extract_doy + , subq_14.metric_time__day + , subq_14.metric_time__week + , subq_14.metric_time__month + , subq_14.metric_time__quarter + , subq_14.metric_time__year + , subq_14.metric_time__extract_year + , subq_14.metric_time__extract_quarter + , subq_14.metric_time__extract_month + , subq_14.metric_time__extract_day + , subq_14.metric_time__extract_dow + , subq_14.metric_time__extract_doy + , subq_14.user + , subq_14.session + , subq_14.visit__user + , subq_14.visit__session + , subq_14.referrer_id + , subq_14.visit__referrer_id + , subq_14.user__home_state_latest + , subq_14.visits + , subq_14.visitors FROM ( - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'user__home_state_latest', 'ds__day', 'metric_time__day', 'user'] + -- Join Standard Outputs SELECT - subq_14.ds__day - , subq_14.metric_time__day - , subq_14.user - , subq_14.visit__referrer_id - , subq_14.user__home_state_latest - , subq_14.visits + subq_11.ds__day AS ds__day + , subq_11.ds__week AS ds__week + , subq_11.ds__month AS ds__month + , subq_11.ds__quarter AS ds__quarter + , subq_11.ds__year AS ds__year + , subq_11.ds__extract_year AS ds__extract_year + , subq_11.ds__extract_quarter AS ds__extract_quarter + , subq_11.ds__extract_month AS ds__extract_month + , subq_11.ds__extract_day AS ds__extract_day + , subq_11.ds__extract_dow AS ds__extract_dow + , subq_11.ds__extract_doy AS ds__extract_doy + , subq_11.visit__ds__day AS visit__ds__day + , subq_11.visit__ds__week AS visit__ds__week + , subq_11.visit__ds__month AS visit__ds__month + , subq_11.visit__ds__quarter AS visit__ds__quarter + , subq_11.visit__ds__year AS visit__ds__year + , subq_11.visit__ds__extract_year AS visit__ds__extract_year + , subq_11.visit__ds__extract_quarter AS visit__ds__extract_quarter + , subq_11.visit__ds__extract_month AS visit__ds__extract_month + , subq_11.visit__ds__extract_day AS visit__ds__extract_day + , subq_11.visit__ds__extract_dow AS visit__ds__extract_dow + , subq_11.visit__ds__extract_doy AS visit__ds__extract_doy + , subq_11.metric_time__day AS metric_time__day + , subq_11.metric_time__week AS metric_time__week + , subq_11.metric_time__month AS metric_time__month + , subq_11.metric_time__quarter AS metric_time__quarter + , subq_11.metric_time__year AS metric_time__year + , subq_11.metric_time__extract_year AS metric_time__extract_year + , subq_11.metric_time__extract_quarter AS metric_time__extract_quarter + , subq_11.metric_time__extract_month AS metric_time__extract_month + , subq_11.metric_time__extract_day AS metric_time__extract_day + , subq_11.metric_time__extract_dow AS metric_time__extract_dow + , subq_11.metric_time__extract_doy AS metric_time__extract_doy + , subq_11.user AS user + , subq_11.session AS session + , subq_11.visit__user AS visit__user + , subq_11.visit__session AS visit__session + , subq_11.referrer_id AS referrer_id + , subq_11.visit__referrer_id AS visit__referrer_id + , subq_13.home_state_latest AS user__home_state_latest + , subq_11.visits AS visits + , subq_11.visitors AS visitors FROM ( - -- Join Standard Outputs + -- Metric Time Dimension 'ds' SELECT - subq_11.ds__day AS ds__day - , subq_11.ds__week AS ds__week - , subq_11.ds__month AS ds__month - , subq_11.ds__quarter AS ds__quarter - , subq_11.ds__year AS ds__year - , subq_11.ds__extract_year AS ds__extract_year - , subq_11.ds__extract_quarter AS ds__extract_quarter - , subq_11.ds__extract_month AS ds__extract_month - , subq_11.ds__extract_day AS ds__extract_day - , subq_11.ds__extract_dow AS ds__extract_dow - , subq_11.ds__extract_doy AS ds__extract_doy - , subq_11.visit__ds__day AS visit__ds__day - , subq_11.visit__ds__week AS visit__ds__week - , subq_11.visit__ds__month AS visit__ds__month - , subq_11.visit__ds__quarter AS visit__ds__quarter - , subq_11.visit__ds__year AS visit__ds__year - , subq_11.visit__ds__extract_year AS visit__ds__extract_year - , subq_11.visit__ds__extract_quarter AS visit__ds__extract_quarter - , subq_11.visit__ds__extract_month AS visit__ds__extract_month - , subq_11.visit__ds__extract_day AS visit__ds__extract_day - , subq_11.visit__ds__extract_dow AS visit__ds__extract_dow - , subq_11.visit__ds__extract_doy AS visit__ds__extract_doy - , subq_11.metric_time__day AS metric_time__day - , subq_11.metric_time__week AS metric_time__week - , subq_11.metric_time__month AS metric_time__month - , subq_11.metric_time__quarter AS metric_time__quarter - , subq_11.metric_time__year AS metric_time__year - , subq_11.metric_time__extract_year AS metric_time__extract_year - , subq_11.metric_time__extract_quarter AS metric_time__extract_quarter - , subq_11.metric_time__extract_month AS metric_time__extract_month - , subq_11.metric_time__extract_day AS metric_time__extract_day - , subq_11.metric_time__extract_dow AS metric_time__extract_dow - , subq_11.metric_time__extract_doy AS metric_time__extract_doy - , subq_11.user AS user - , subq_11.session AS session - , subq_11.visit__user AS visit__user - , subq_11.visit__session AS visit__session - , subq_11.referrer_id AS referrer_id - , subq_11.visit__referrer_id AS visit__referrer_id - , subq_13.home_state_latest AS user__home_state_latest - , subq_11.visits AS visits - , subq_11.visitors AS visitors + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.visit__ds__day + , subq_10.visit__ds__week + , subq_10.visit__ds__month + , subq_10.visit__ds__quarter + , subq_10.visit__ds__year + , subq_10.visit__ds__extract_year + , subq_10.visit__ds__extract_quarter + , subq_10.visit__ds__extract_month + , subq_10.visit__ds__extract_day + , subq_10.visit__ds__extract_dow + , subq_10.visit__ds__extract_doy + , subq_10.ds__day AS metric_time__day + , subq_10.ds__week AS metric_time__week + , subq_10.ds__month AS metric_time__month + , subq_10.ds__quarter AS metric_time__quarter + , subq_10.ds__year AS metric_time__year + , subq_10.ds__extract_year AS metric_time__extract_year + , subq_10.ds__extract_quarter AS metric_time__extract_quarter + , subq_10.ds__extract_month AS metric_time__extract_month + , subq_10.ds__extract_day AS metric_time__extract_day + , subq_10.ds__extract_dow AS metric_time__extract_dow + , subq_10.ds__extract_doy AS metric_time__extract_doy + , subq_10.user + , subq_10.session + , subq_10.visit__user + , subq_10.visit__session + , subq_10.referrer_id + , subq_10.visit__referrer_id + , subq_10.visits + , subq_10.visitors FROM ( - -- Metric Time Dimension 'ds' - SELECT - subq_10.ds__day - , subq_10.ds__week - , subq_10.ds__month - , subq_10.ds__quarter - , subq_10.ds__year - , subq_10.ds__extract_year - , subq_10.ds__extract_quarter - , subq_10.ds__extract_month - , subq_10.ds__extract_day - , subq_10.ds__extract_dow - , subq_10.ds__extract_doy - , subq_10.visit__ds__day - , subq_10.visit__ds__week - , subq_10.visit__ds__month - , subq_10.visit__ds__quarter - , subq_10.visit__ds__year - , subq_10.visit__ds__extract_year - , subq_10.visit__ds__extract_quarter - , subq_10.visit__ds__extract_month - , subq_10.visit__ds__extract_day - , subq_10.visit__ds__extract_dow - , subq_10.visit__ds__extract_doy - , subq_10.ds__day AS metric_time__day - , subq_10.ds__week AS metric_time__week - , subq_10.ds__month AS metric_time__month - , subq_10.ds__quarter AS metric_time__quarter - , subq_10.ds__year AS metric_time__year - , subq_10.ds__extract_year AS metric_time__extract_year - , subq_10.ds__extract_quarter AS metric_time__extract_quarter - , subq_10.ds__extract_month AS metric_time__extract_month - , subq_10.ds__extract_day AS metric_time__extract_day - , subq_10.ds__extract_dow AS metric_time__extract_dow - , subq_10.ds__extract_doy AS metric_time__extract_doy - , subq_10.user - , subq_10.session - , subq_10.visit__user - , subq_10.visit__session - , subq_10.referrer_id - , subq_10.visit__referrer_id - , subq_10.visits - , subq_10.visitors - FROM ( - -- Read Elements From Semantic Model 'visits_source' - SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_10 - ) subq_11 - LEFT OUTER JOIN ( - -- Pass Only Elements: ['home_state_latest', 'user'] + -- Read Elements From Semantic Model 'visits_source' SELECT - subq_12.user - , subq_12.home_state_latest - FROM ( - -- Read Elements From Semantic Model 'users_latest' - SELECT - DATE_TRUNC('day', users_latest_src_28000.ds) AS ds_latest__day - , DATE_TRUNC('week', users_latest_src_28000.ds) AS ds_latest__week - , DATE_TRUNC('month', users_latest_src_28000.ds) AS ds_latest__month - , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS ds_latest__quarter - , DATE_TRUNC('year', users_latest_src_28000.ds) AS ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS ds_latest__extract_day - , CASE WHEN EXTRACT(dow FROM users_latest_src_28000.ds) = 0 THEN EXTRACT(dow FROM users_latest_src_28000.ds) + 7 ELSE EXTRACT(dow FROM users_latest_src_28000.ds) END AS ds_latest__extract_dow - , EXTRACT(doy FROM users_latest_src_28000.ds) AS ds_latest__extract_doy - , users_latest_src_28000.home_state_latest - , DATE_TRUNC('day', users_latest_src_28000.ds) AS user__ds_latest__day - , DATE_TRUNC('week', users_latest_src_28000.ds) AS user__ds_latest__week - , DATE_TRUNC('month', users_latest_src_28000.ds) AS user__ds_latest__month - , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS user__ds_latest__quarter - , DATE_TRUNC('year', users_latest_src_28000.ds) AS user__ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS user__ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS user__ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS user__ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS user__ds_latest__extract_day - , CASE WHEN EXTRACT(dow FROM users_latest_src_28000.ds) = 0 THEN EXTRACT(dow FROM users_latest_src_28000.ds) + 7 ELSE EXTRACT(dow FROM users_latest_src_28000.ds) END AS user__ds_latest__extract_dow - , EXTRACT(doy FROM users_latest_src_28000.ds) AS user__ds_latest__extract_doy - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , users_latest_src_28000.user_id AS user - FROM ***************************.dim_users_latest users_latest_src_28000 - ) subq_12 - ) subq_13 - ON - subq_11.user = subq_13.user - ) subq_14 - ) subq_15 - INNER JOIN ( - -- Add column with generated UUID - SELECT - subq_17.ds__day - , subq_17.ds__week - , subq_17.ds__month - , subq_17.ds__quarter - , subq_17.ds__year - , subq_17.ds__extract_year - , subq_17.ds__extract_quarter - , subq_17.ds__extract_month - , subq_17.ds__extract_day - , subq_17.ds__extract_dow - , subq_17.ds__extract_doy - , subq_17.buy__ds__day - , subq_17.buy__ds__week - , subq_17.buy__ds__month - , subq_17.buy__ds__quarter - , subq_17.buy__ds__year - , subq_17.buy__ds__extract_year - , subq_17.buy__ds__extract_quarter - , subq_17.buy__ds__extract_month - , subq_17.buy__ds__extract_day - , subq_17.buy__ds__extract_dow - , subq_17.buy__ds__extract_doy - , subq_17.metric_time__day - , subq_17.metric_time__week - , subq_17.metric_time__month - , subq_17.metric_time__quarter - , subq_17.metric_time__year - , subq_17.metric_time__extract_year - , subq_17.metric_time__extract_quarter - , subq_17.metric_time__extract_month - , subq_17.metric_time__extract_day - , subq_17.metric_time__extract_dow - , subq_17.metric_time__extract_doy - , subq_17.user - , subq_17.session_id - , subq_17.buy__user - , subq_17.buy__session_id - , subq_17.buys - , subq_17.buyers - , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) AS mf_internal_uuid - FROM ( - -- Metric Time Dimension 'ds' + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_28000.ds) END AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_10 + ) subq_11 + LEFT OUTER JOIN ( + -- Pass Only Elements: ['home_state_latest', 'user'] SELECT - subq_16.ds__day - , subq_16.ds__week - , subq_16.ds__month - , subq_16.ds__quarter - , subq_16.ds__year - , subq_16.ds__extract_year - , subq_16.ds__extract_quarter - , subq_16.ds__extract_month - , subq_16.ds__extract_day - , subq_16.ds__extract_dow - , subq_16.ds__extract_doy - , subq_16.buy__ds__day - , subq_16.buy__ds__week - , subq_16.buy__ds__month - , subq_16.buy__ds__quarter - , subq_16.buy__ds__year - , subq_16.buy__ds__extract_year - , subq_16.buy__ds__extract_quarter - , subq_16.buy__ds__extract_month - , subq_16.buy__ds__extract_day - , subq_16.buy__ds__extract_dow - , subq_16.buy__ds__extract_doy - , subq_16.ds__day AS metric_time__day - , subq_16.ds__week AS metric_time__week - , subq_16.ds__month AS metric_time__month - , subq_16.ds__quarter AS metric_time__quarter - , subq_16.ds__year AS metric_time__year - , subq_16.ds__extract_year AS metric_time__extract_year - , subq_16.ds__extract_quarter AS metric_time__extract_quarter - , subq_16.ds__extract_month AS metric_time__extract_month - , subq_16.ds__extract_day AS metric_time__extract_day - , subq_16.ds__extract_dow AS metric_time__extract_dow - , subq_16.ds__extract_doy AS metric_time__extract_doy - , subq_16.user - , subq_16.session_id - , subq_16.buy__user - , subq_16.buy__session_id - , subq_16.buys - , subq_16.buyers + subq_12.user + , subq_12.home_state_latest FROM ( - -- Read Elements From Semantic Model 'buys_source' + -- Read Elements From Semantic Model 'users_latest' SELECT - 1 AS buys - , buys_source_src_28000.user_id AS buyers - , DATE_TRUNC('day', buys_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', buys_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', buys_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', buys_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day - , CASE WHEN EXTRACT(dow FROM buys_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM buys_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM buys_source_src_28000.ds) END AS ds__extract_dow - , EXTRACT(doy FROM buys_source_src_28000.ds) AS ds__extract_doy - , DATE_TRUNC('day', buys_source_src_28000.ds) AS buy__ds__day - , DATE_TRUNC('week', buys_source_src_28000.ds) AS buy__ds__week - , DATE_TRUNC('month', buys_source_src_28000.ds) AS buy__ds__month - , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS buy__ds__quarter - , DATE_TRUNC('year', buys_source_src_28000.ds) AS buy__ds__year - , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year - , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter - , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month - , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day - , CASE WHEN EXTRACT(dow FROM buys_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM buys_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM buys_source_src_28000.ds) END AS buy__ds__extract_dow - , EXTRACT(doy FROM buys_source_src_28000.ds) AS buy__ds__extract_doy - , buys_source_src_28000.user_id AS user - , buys_source_src_28000.session_id - , buys_source_src_28000.user_id AS buy__user - , buys_source_src_28000.session_id AS buy__session_id - FROM ***************************.fct_buys buys_source_src_28000 - ) subq_16 - ) subq_17 - ) subq_18 - ON - ( - subq_15.user = subq_18.user - ) AND ( - ( - subq_15.ds__day <= subq_18.ds__day - ) AND ( - subq_15.ds__day > DATEADD(day, -7, subq_18.ds__day) - ) - ) - ) subq_19 - ) subq_20 - ) subq_21 - LEFT OUTER JOIN ( - -- Pass Only Elements: ['home_state_latest', 'user'] - SELECT - subq_22.user - , subq_22.home_state_latest - FROM ( - -- Read Elements From Semantic Model 'users_latest' + DATE_TRUNC('day', users_latest_src_28000.ds) AS ds_latest__day + , DATE_TRUNC('week', users_latest_src_28000.ds) AS ds_latest__week + , DATE_TRUNC('month', users_latest_src_28000.ds) AS ds_latest__month + , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS ds_latest__quarter + , DATE_TRUNC('year', users_latest_src_28000.ds) AS ds_latest__year + , EXTRACT(year FROM users_latest_src_28000.ds) AS ds_latest__extract_year + , EXTRACT(quarter FROM users_latest_src_28000.ds) AS ds_latest__extract_quarter + , EXTRACT(month FROM users_latest_src_28000.ds) AS ds_latest__extract_month + , EXTRACT(day FROM users_latest_src_28000.ds) AS ds_latest__extract_day + , CASE WHEN EXTRACT(dow FROM users_latest_src_28000.ds) = 0 THEN EXTRACT(dow FROM users_latest_src_28000.ds) + 7 ELSE EXTRACT(dow FROM users_latest_src_28000.ds) END AS ds_latest__extract_dow + , EXTRACT(doy FROM users_latest_src_28000.ds) AS ds_latest__extract_doy + , users_latest_src_28000.home_state_latest + , DATE_TRUNC('day', users_latest_src_28000.ds) AS user__ds_latest__day + , DATE_TRUNC('week', users_latest_src_28000.ds) AS user__ds_latest__week + , DATE_TRUNC('month', users_latest_src_28000.ds) AS user__ds_latest__month + , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS user__ds_latest__quarter + , DATE_TRUNC('year', users_latest_src_28000.ds) AS user__ds_latest__year + , EXTRACT(year FROM users_latest_src_28000.ds) AS user__ds_latest__extract_year + , EXTRACT(quarter FROM users_latest_src_28000.ds) AS user__ds_latest__extract_quarter + , EXTRACT(month FROM users_latest_src_28000.ds) AS user__ds_latest__extract_month + , EXTRACT(day FROM users_latest_src_28000.ds) AS user__ds_latest__extract_day + , CASE WHEN EXTRACT(dow FROM users_latest_src_28000.ds) = 0 THEN EXTRACT(dow FROM users_latest_src_28000.ds) + 7 ELSE EXTRACT(dow FROM users_latest_src_28000.ds) END AS user__ds_latest__extract_dow + , EXTRACT(doy FROM users_latest_src_28000.ds) AS user__ds_latest__extract_doy + , users_latest_src_28000.home_state_latest AS user__home_state_latest + , users_latest_src_28000.user_id AS user + FROM ***************************.dim_users_latest users_latest_src_28000 + ) subq_12 + ) subq_13 + ON + subq_11.user = subq_13.user + ) subq_14 + WHERE visit__referrer_id = '123456' + ) subq_15 + ) subq_16 + INNER JOIN ( + -- Add column with generated UUID SELECT - DATE_TRUNC('day', users_latest_src_28000.ds) AS ds_latest__day - , DATE_TRUNC('week', users_latest_src_28000.ds) AS ds_latest__week - , DATE_TRUNC('month', users_latest_src_28000.ds) AS ds_latest__month - , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS ds_latest__quarter - , DATE_TRUNC('year', users_latest_src_28000.ds) AS ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS ds_latest__extract_day - , CASE WHEN EXTRACT(dow FROM users_latest_src_28000.ds) = 0 THEN EXTRACT(dow FROM users_latest_src_28000.ds) + 7 ELSE EXTRACT(dow FROM users_latest_src_28000.ds) END AS ds_latest__extract_dow - , EXTRACT(doy FROM users_latest_src_28000.ds) AS ds_latest__extract_doy - , users_latest_src_28000.home_state_latest - , DATE_TRUNC('day', users_latest_src_28000.ds) AS user__ds_latest__day - , DATE_TRUNC('week', users_latest_src_28000.ds) AS user__ds_latest__week - , DATE_TRUNC('month', users_latest_src_28000.ds) AS user__ds_latest__month - , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS user__ds_latest__quarter - , DATE_TRUNC('year', users_latest_src_28000.ds) AS user__ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS user__ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS user__ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS user__ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS user__ds_latest__extract_day - , CASE WHEN EXTRACT(dow FROM users_latest_src_28000.ds) = 0 THEN EXTRACT(dow FROM users_latest_src_28000.ds) + 7 ELSE EXTRACT(dow FROM users_latest_src_28000.ds) END AS user__ds_latest__extract_dow - , EXTRACT(doy FROM users_latest_src_28000.ds) AS user__ds_latest__extract_doy - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , users_latest_src_28000.user_id AS user - FROM ***************************.dim_users_latest users_latest_src_28000 - ) subq_22 - ) subq_23 - ON - subq_21.user = subq_23.user - ) subq_24 - ) subq_25 + subq_18.ds__day + , subq_18.ds__week + , subq_18.ds__month + , subq_18.ds__quarter + , subq_18.ds__year + , subq_18.ds__extract_year + , subq_18.ds__extract_quarter + , subq_18.ds__extract_month + , subq_18.ds__extract_day + , subq_18.ds__extract_dow + , subq_18.ds__extract_doy + , subq_18.buy__ds__day + , subq_18.buy__ds__week + , subq_18.buy__ds__month + , subq_18.buy__ds__quarter + , subq_18.buy__ds__year + , subq_18.buy__ds__extract_year + , subq_18.buy__ds__extract_quarter + , subq_18.buy__ds__extract_month + , subq_18.buy__ds__extract_day + , subq_18.buy__ds__extract_dow + , subq_18.buy__ds__extract_doy + , subq_18.metric_time__day + , subq_18.metric_time__week + , subq_18.metric_time__month + , subq_18.metric_time__quarter + , subq_18.metric_time__year + , subq_18.metric_time__extract_year + , subq_18.metric_time__extract_quarter + , subq_18.metric_time__extract_month + , subq_18.metric_time__extract_day + , subq_18.metric_time__extract_dow + , subq_18.metric_time__extract_doy + , subq_18.user + , subq_18.session_id + , subq_18.buy__user + , subq_18.buy__session_id + , subq_18.buys + , subq_18.buyers + , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_17.ds__day + , subq_17.ds__week + , subq_17.ds__month + , subq_17.ds__quarter + , subq_17.ds__year + , subq_17.ds__extract_year + , subq_17.ds__extract_quarter + , subq_17.ds__extract_month + , subq_17.ds__extract_day + , subq_17.ds__extract_dow + , subq_17.ds__extract_doy + , subq_17.buy__ds__day + , subq_17.buy__ds__week + , subq_17.buy__ds__month + , subq_17.buy__ds__quarter + , subq_17.buy__ds__year + , subq_17.buy__ds__extract_year + , subq_17.buy__ds__extract_quarter + , subq_17.buy__ds__extract_month + , subq_17.buy__ds__extract_day + , subq_17.buy__ds__extract_dow + , subq_17.buy__ds__extract_doy + , subq_17.ds__day AS metric_time__day + , subq_17.ds__week AS metric_time__week + , subq_17.ds__month AS metric_time__month + , subq_17.ds__quarter AS metric_time__quarter + , subq_17.ds__year AS metric_time__year + , subq_17.ds__extract_year AS metric_time__extract_year + , subq_17.ds__extract_quarter AS metric_time__extract_quarter + , subq_17.ds__extract_month AS metric_time__extract_month + , subq_17.ds__extract_day AS metric_time__extract_day + , subq_17.ds__extract_dow AS metric_time__extract_dow + , subq_17.ds__extract_doy AS metric_time__extract_doy + , subq_17.user + , subq_17.session_id + , subq_17.buy__user + , subq_17.buy__session_id + , subq_17.buys + , subq_17.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_28000.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM buys_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM buys_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM buys_source_src_28000.ds) END AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_28000.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day + , CASE WHEN EXTRACT(dow FROM buys_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM buys_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM buys_source_src_28000.ds) END AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS buy__ds__extract_doy + , buys_source_src_28000.user_id AS user + , buys_source_src_28000.session_id + , buys_source_src_28000.user_id AS buy__user + , buys_source_src_28000.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_17 + ) subq_18 + ) subq_19 + ON + ( + subq_16.user = subq_19.user + ) AND ( + ( + subq_16.ds__day <= subq_19.ds__day + ) AND ( + subq_16.ds__day > DATEADD(day, -7, subq_19.ds__day) + ) + ) + ) subq_20 + ) subq_21 + ) subq_22 GROUP BY - subq_25.metric_time__day - , subq_25.user__home_state_latest - ) subq_26 + subq_22.metric_time__day + , subq_22.user__home_state_latest + ) subq_23 ON ( - subq_9.user__home_state_latest = subq_26.user__home_state_latest + subq_9.user__home_state_latest = subq_23.user__home_state_latest ) AND ( - subq_9.metric_time__day = subq_26.metric_time__day + subq_9.metric_time__day = subq_23.metric_time__day ) GROUP BY - COALESCE(subq_9.metric_time__day, subq_26.metric_time__day) - , COALESCE(subq_9.user__home_state_latest, subq_26.user__home_state_latest) -) subq_27 + COALESCE(subq_9.metric_time__day, subq_23.metric_time__day) + , COALESCE(subq_9.user__home_state_latest, subq_23.user__home_state_latest) +) subq_24 diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_query_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_query_filters__plan0_optimized.sql index 21e5bde3f3..d6ad52c13c 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_query_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_conversion_metric_query_filters__plan0_optimized.sql @@ -6,10 +6,10 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_37.metric_time__day, subq_54.metric_time__day) AS metric_time__day - , COALESCE(subq_37.user__home_state_latest, subq_54.user__home_state_latest) AS user__home_state_latest - , MAX(subq_37.visits) AS visits - , MAX(subq_54.buys) AS buys + COALESCE(subq_34.metric_time__day, subq_48.metric_time__day) AS metric_time__day + , COALESCE(subq_34.user__home_state_latest, subq_48.user__home_state_latest) AS user__home_state_latest + , MAX(subq_34.visits) AS visits + , MAX(subq_48.buys) AS buys FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'user__home_state_latest', 'metric_time__day'] @@ -22,10 +22,10 @@ FROM ( -- Join Standard Outputs -- Pass Only Elements: ['visits', 'user__home_state_latest', 'visit__referrer_id', 'metric_time__day'] SELECT - subq_30.metric_time__day AS metric_time__day - , subq_30.visit__referrer_id AS visit__referrer_id + subq_27.metric_time__day AS metric_time__day + , subq_27.visit__referrer_id AS visit__referrer_id , users_latest_src_28000.home_state_latest AS user__home_state_latest - , subq_30.visits AS visits + , subq_27.visits AS visits FROM ( -- Read Elements From Semantic Model 'visits_source' -- Metric Time Dimension 'ds' @@ -36,104 +36,115 @@ FROM ( , referrer_id AS visit__referrer_id , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_30 + ) subq_27 LEFT OUTER JOIN ***************************.dim_users_latest users_latest_src_28000 ON - subq_30.user = users_latest_src_28000.user_id - ) subq_34 + subq_27.user = users_latest_src_28000.user_id + ) subq_31 WHERE visit__referrer_id = '123456' GROUP BY metric_time__day , user__home_state_latest - ) subq_37 + ) subq_34 FULL OUTER JOIN ( - -- Join Standard Outputs + -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'user__home_state_latest', 'metric_time__day'] -- Aggregate Measures SELECT - subq_47.metric_time__day AS metric_time__day - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , SUM(subq_47.buys) AS buys + metric_time__day + , user__home_state_latest + , SUM(buys) AS buys FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_43.visits) OVER ( + FIRST_VALUE(subq_41.visits) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_43.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_41.visit__referrer_id) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_43.user__home_state_latest) OVER ( + , FIRST_VALUE(subq_41.user__home_state_latest) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user__home_state_latest - , FIRST_VALUE(subq_43.ds__day) OVER ( + , FIRST_VALUE(subq_41.ds__day) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_43.metric_time__day) OVER ( + , FIRST_VALUE(subq_41.metric_time__day) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_43.user) OVER ( + , FIRST_VALUE(subq_41.user) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_46.mf_internal_uuid AS mf_internal_uuid - , subq_46.buys AS buys + , subq_44.mf_internal_uuid AS mf_internal_uuid + , subq_44.buys AS buys FROM ( - -- Join Standard Outputs + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'user__home_state_latest', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_39.ds__day AS ds__day - , subq_39.metric_time__day AS metric_time__day - , subq_39.user AS user - , subq_39.visit__referrer_id AS visit__referrer_id - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , subq_39.visits AS visits + ds__day + , metric_time__day + , subq_39.user + , visit__referrer_id + , user__home_state_latest + , visits FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 + subq_36.ds__day AS ds__day + , subq_36.metric_time__day AS metric_time__day + , subq_36.user AS user + , subq_36.visit__referrer_id AS visit__referrer_id + , users_latest_src_28000.home_state_latest AS user__home_state_latest + , subq_36.visits AS visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_36 + LEFT OUTER JOIN + ***************************.dim_users_latest users_latest_src_28000 + ON + subq_36.user = users_latest_src_28000.user_id ) subq_39 - LEFT OUTER JOIN - ***************************.dim_users_latest users_latest_src_28000 - ON - subq_39.user = users_latest_src_28000.user_id - ) subq_43 + WHERE visit__referrer_id = '123456' + ) subq_41 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -144,33 +155,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_46 + ) subq_44 ON ( - subq_43.user = subq_46.user + subq_41.user = subq_44.user ) AND ( ( - subq_43.ds__day <= subq_46.ds__day + subq_41.ds__day <= subq_44.ds__day ) AND ( - subq_43.ds__day > DATEADD(day, -7, subq_46.ds__day) + subq_41.ds__day > DATEADD(day, -7, subq_44.ds__day) ) ) - ) subq_47 - LEFT OUTER JOIN - ***************************.dim_users_latest users_latest_src_28000 - ON - subq_47.user = users_latest_src_28000.user_id + ) subq_45 GROUP BY - subq_47.metric_time__day - , users_latest_src_28000.home_state_latest - ) subq_54 + metric_time__day + , user__home_state_latest + ) subq_48 ON ( - subq_37.user__home_state_latest = subq_54.user__home_state_latest + subq_34.user__home_state_latest = subq_48.user__home_state_latest ) AND ( - subq_37.metric_time__day = subq_54.metric_time__day + subq_34.metric_time__day = subq_48.metric_time__day ) GROUP BY - COALESCE(subq_37.metric_time__day, subq_54.metric_time__day) - , COALESCE(subq_37.user__home_state_latest, subq_54.user__home_state_latest) -) subq_55 + COALESCE(subq_34.metric_time__day, subq_48.metric_time__day) + , COALESCE(subq_34.user__home_state_latest, subq_48.user__home_state_latest) +) subq_49 diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_query_filters__plan0.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_query_filters__plan0.sql index e21168d214..35cb89c7c5 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_query_filters__plan0.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_query_filters__plan0.sql @@ -1,15 +1,15 @@ -- Compute Metrics via Expressions SELECT - subq_27.metric_time__day - , subq_27.user__home_state_latest - , CAST(subq_27.buys AS DOUBLE) / CAST(NULLIF(subq_27.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days + subq_24.metric_time__day + , subq_24.user__home_state_latest + , CAST(subq_24.buys AS DOUBLE) / CAST(NULLIF(subq_24.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_9.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , COALESCE(subq_9.user__home_state_latest, subq_26.user__home_state_latest) AS user__home_state_latest + COALESCE(subq_9.metric_time__day, subq_23.metric_time__day) AS metric_time__day + , COALESCE(subq_9.user__home_state_latest, subq_23.user__home_state_latest) AS user__home_state_latest , MAX(subq_9.visits) AS visits - , MAX(subq_26.buys) AS buys + , MAX(subq_23.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -182,451 +182,443 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_25.metric_time__day - , subq_25.user__home_state_latest - , SUM(subq_25.buys) AS buys + subq_22.metric_time__day + , subq_22.user__home_state_latest + , SUM(subq_22.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'user__home_state_latest', 'metric_time__day'] SELECT - subq_24.metric_time__day - , subq_24.user__home_state_latest - , subq_24.buys + subq_21.metric_time__day + , subq_21.user__home_state_latest + , subq_21.buys FROM ( - -- Join Standard Outputs + -- Find conversions for user within the range of 7 day SELECT - subq_21.metric_time__day AS metric_time__day - , subq_21.user AS user - , subq_21.visit__referrer_id AS visit__referrer_id - , subq_23.home_state_latest AS user__home_state_latest - , subq_21.buys AS buys + subq_20.ds__day + , subq_20.metric_time__day + , subq_20.user + , subq_20.visit__referrer_id + , subq_20.user__home_state_latest + , subq_20.buys + , subq_20.visits FROM ( - -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day', 'user'] - SELECT - subq_20.metric_time__day - , subq_20.user - , subq_20.visit__referrer_id - , subq_20.buys + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_16.visits) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_16.visit__referrer_id) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visit__referrer_id + , FIRST_VALUE(subq_16.user__home_state_latest) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user__home_state_latest + , FIRST_VALUE(subq_16.ds__day) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_16.metric_time__day) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS metric_time__day + , FIRST_VALUE(subq_16.user) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_19.mf_internal_uuid AS mf_internal_uuid + , subq_19.buys AS buys FROM ( - -- Find conversions for user within the range of 7 day + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'user__home_state_latest', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_19.ds__day - , subq_19.metric_time__day - , subq_19.user - , subq_19.visit__referrer_id - , subq_19.user__home_state_latest - , subq_19.buys - , subq_19.visits + subq_15.ds__day + , subq_15.metric_time__day + , subq_15.user + , subq_15.visit__referrer_id + , subq_15.user__home_state_latest + , subq_15.visits FROM ( - -- Dedupe the fanout with mf_internal_uuid in the conversion data set - SELECT DISTINCT - FIRST_VALUE(subq_15.visits) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS visits - , FIRST_VALUE(subq_15.visit__referrer_id) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS visit__referrer_id - , FIRST_VALUE(subq_15.user__home_state_latest) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS user__home_state_latest - , FIRST_VALUE(subq_15.ds__day) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS ds__day - , FIRST_VALUE(subq_15.metric_time__day) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS metric_time__day - , FIRST_VALUE(subq_15.user) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS user - , subq_18.mf_internal_uuid AS mf_internal_uuid - , subq_18.buys AS buys + -- Constrain Output with WHERE + SELECT + subq_14.ds__day + , subq_14.ds__week + , subq_14.ds__month + , subq_14.ds__quarter + , subq_14.ds__year + , subq_14.ds__extract_year + , subq_14.ds__extract_quarter + , subq_14.ds__extract_month + , subq_14.ds__extract_day + , subq_14.ds__extract_dow + , subq_14.ds__extract_doy + , subq_14.visit__ds__day + , subq_14.visit__ds__week + , subq_14.visit__ds__month + , subq_14.visit__ds__quarter + , subq_14.visit__ds__year + , subq_14.visit__ds__extract_year + , subq_14.visit__ds__extract_quarter + , subq_14.visit__ds__extract_month + , subq_14.visit__ds__extract_day + , subq_14.visit__ds__extract_dow + , subq_14.visit__ds__extract_doy + , subq_14.metric_time__day + , subq_14.metric_time__week + , subq_14.metric_time__month + , subq_14.metric_time__quarter + , subq_14.metric_time__year + , subq_14.metric_time__extract_year + , subq_14.metric_time__extract_quarter + , subq_14.metric_time__extract_month + , subq_14.metric_time__extract_day + , subq_14.metric_time__extract_dow + , subq_14.metric_time__extract_doy + , subq_14.user + , subq_14.session + , subq_14.visit__user + , subq_14.visit__session + , subq_14.referrer_id + , subq_14.visit__referrer_id + , subq_14.user__home_state_latest + , subq_14.visits + , subq_14.visitors FROM ( - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'user__home_state_latest', 'ds__day', 'metric_time__day', 'user'] + -- Join Standard Outputs SELECT - subq_14.ds__day - , subq_14.metric_time__day - , subq_14.user - , subq_14.visit__referrer_id - , subq_14.user__home_state_latest - , subq_14.visits + subq_11.ds__day AS ds__day + , subq_11.ds__week AS ds__week + , subq_11.ds__month AS ds__month + , subq_11.ds__quarter AS ds__quarter + , subq_11.ds__year AS ds__year + , subq_11.ds__extract_year AS ds__extract_year + , subq_11.ds__extract_quarter AS ds__extract_quarter + , subq_11.ds__extract_month AS ds__extract_month + , subq_11.ds__extract_day AS ds__extract_day + , subq_11.ds__extract_dow AS ds__extract_dow + , subq_11.ds__extract_doy AS ds__extract_doy + , subq_11.visit__ds__day AS visit__ds__day + , subq_11.visit__ds__week AS visit__ds__week + , subq_11.visit__ds__month AS visit__ds__month + , subq_11.visit__ds__quarter AS visit__ds__quarter + , subq_11.visit__ds__year AS visit__ds__year + , subq_11.visit__ds__extract_year AS visit__ds__extract_year + , subq_11.visit__ds__extract_quarter AS visit__ds__extract_quarter + , subq_11.visit__ds__extract_month AS visit__ds__extract_month + , subq_11.visit__ds__extract_day AS visit__ds__extract_day + , subq_11.visit__ds__extract_dow AS visit__ds__extract_dow + , subq_11.visit__ds__extract_doy AS visit__ds__extract_doy + , subq_11.metric_time__day AS metric_time__day + , subq_11.metric_time__week AS metric_time__week + , subq_11.metric_time__month AS metric_time__month + , subq_11.metric_time__quarter AS metric_time__quarter + , subq_11.metric_time__year AS metric_time__year + , subq_11.metric_time__extract_year AS metric_time__extract_year + , subq_11.metric_time__extract_quarter AS metric_time__extract_quarter + , subq_11.metric_time__extract_month AS metric_time__extract_month + , subq_11.metric_time__extract_day AS metric_time__extract_day + , subq_11.metric_time__extract_dow AS metric_time__extract_dow + , subq_11.metric_time__extract_doy AS metric_time__extract_doy + , subq_11.user AS user + , subq_11.session AS session + , subq_11.visit__user AS visit__user + , subq_11.visit__session AS visit__session + , subq_11.referrer_id AS referrer_id + , subq_11.visit__referrer_id AS visit__referrer_id + , subq_13.home_state_latest AS user__home_state_latest + , subq_11.visits AS visits + , subq_11.visitors AS visitors FROM ( - -- Join Standard Outputs + -- Metric Time Dimension 'ds' SELECT - subq_11.ds__day AS ds__day - , subq_11.ds__week AS ds__week - , subq_11.ds__month AS ds__month - , subq_11.ds__quarter AS ds__quarter - , subq_11.ds__year AS ds__year - , subq_11.ds__extract_year AS ds__extract_year - , subq_11.ds__extract_quarter AS ds__extract_quarter - , subq_11.ds__extract_month AS ds__extract_month - , subq_11.ds__extract_day AS ds__extract_day - , subq_11.ds__extract_dow AS ds__extract_dow - , subq_11.ds__extract_doy AS ds__extract_doy - , subq_11.visit__ds__day AS visit__ds__day - , subq_11.visit__ds__week AS visit__ds__week - , subq_11.visit__ds__month AS visit__ds__month - , subq_11.visit__ds__quarter AS visit__ds__quarter - , subq_11.visit__ds__year AS visit__ds__year - , subq_11.visit__ds__extract_year AS visit__ds__extract_year - , subq_11.visit__ds__extract_quarter AS visit__ds__extract_quarter - , subq_11.visit__ds__extract_month AS visit__ds__extract_month - , subq_11.visit__ds__extract_day AS visit__ds__extract_day - , subq_11.visit__ds__extract_dow AS visit__ds__extract_dow - , subq_11.visit__ds__extract_doy AS visit__ds__extract_doy - , subq_11.metric_time__day AS metric_time__day - , subq_11.metric_time__week AS metric_time__week - , subq_11.metric_time__month AS metric_time__month - , subq_11.metric_time__quarter AS metric_time__quarter - , subq_11.metric_time__year AS metric_time__year - , subq_11.metric_time__extract_year AS metric_time__extract_year - , subq_11.metric_time__extract_quarter AS metric_time__extract_quarter - , subq_11.metric_time__extract_month AS metric_time__extract_month - , subq_11.metric_time__extract_day AS metric_time__extract_day - , subq_11.metric_time__extract_dow AS metric_time__extract_dow - , subq_11.metric_time__extract_doy AS metric_time__extract_doy - , subq_11.user AS user - , subq_11.session AS session - , subq_11.visit__user AS visit__user - , subq_11.visit__session AS visit__session - , subq_11.referrer_id AS referrer_id - , subq_11.visit__referrer_id AS visit__referrer_id - , subq_13.home_state_latest AS user__home_state_latest - , subq_11.visits AS visits - , subq_11.visitors AS visitors + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.visit__ds__day + , subq_10.visit__ds__week + , subq_10.visit__ds__month + , subq_10.visit__ds__quarter + , subq_10.visit__ds__year + , subq_10.visit__ds__extract_year + , subq_10.visit__ds__extract_quarter + , subq_10.visit__ds__extract_month + , subq_10.visit__ds__extract_day + , subq_10.visit__ds__extract_dow + , subq_10.visit__ds__extract_doy + , subq_10.ds__day AS metric_time__day + , subq_10.ds__week AS metric_time__week + , subq_10.ds__month AS metric_time__month + , subq_10.ds__quarter AS metric_time__quarter + , subq_10.ds__year AS metric_time__year + , subq_10.ds__extract_year AS metric_time__extract_year + , subq_10.ds__extract_quarter AS metric_time__extract_quarter + , subq_10.ds__extract_month AS metric_time__extract_month + , subq_10.ds__extract_day AS metric_time__extract_day + , subq_10.ds__extract_dow AS metric_time__extract_dow + , subq_10.ds__extract_doy AS metric_time__extract_doy + , subq_10.user + , subq_10.session + , subq_10.visit__user + , subq_10.visit__session + , subq_10.referrer_id + , subq_10.visit__referrer_id + , subq_10.visits + , subq_10.visitors FROM ( - -- Metric Time Dimension 'ds' - SELECT - subq_10.ds__day - , subq_10.ds__week - , subq_10.ds__month - , subq_10.ds__quarter - , subq_10.ds__year - , subq_10.ds__extract_year - , subq_10.ds__extract_quarter - , subq_10.ds__extract_month - , subq_10.ds__extract_day - , subq_10.ds__extract_dow - , subq_10.ds__extract_doy - , subq_10.visit__ds__day - , subq_10.visit__ds__week - , subq_10.visit__ds__month - , subq_10.visit__ds__quarter - , subq_10.visit__ds__year - , subq_10.visit__ds__extract_year - , subq_10.visit__ds__extract_quarter - , subq_10.visit__ds__extract_month - , subq_10.visit__ds__extract_day - , subq_10.visit__ds__extract_dow - , subq_10.visit__ds__extract_doy - , subq_10.ds__day AS metric_time__day - , subq_10.ds__week AS metric_time__week - , subq_10.ds__month AS metric_time__month - , subq_10.ds__quarter AS metric_time__quarter - , subq_10.ds__year AS metric_time__year - , subq_10.ds__extract_year AS metric_time__extract_year - , subq_10.ds__extract_quarter AS metric_time__extract_quarter - , subq_10.ds__extract_month AS metric_time__extract_month - , subq_10.ds__extract_day AS metric_time__extract_day - , subq_10.ds__extract_dow AS metric_time__extract_dow - , subq_10.ds__extract_doy AS metric_time__extract_doy - , subq_10.user - , subq_10.session - , subq_10.visit__user - , subq_10.visit__session - , subq_10.referrer_id - , subq_10.visit__referrer_id - , subq_10.visits - , subq_10.visitors - FROM ( - -- Read Elements From Semantic Model 'visits_source' - SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_10 - ) subq_11 - LEFT OUTER JOIN ( - -- Pass Only Elements: ['home_state_latest', 'user'] + -- Read Elements From Semantic Model 'visits_source' SELECT - subq_12.user - , subq_12.home_state_latest - FROM ( - -- Read Elements From Semantic Model 'users_latest' - SELECT - DATE_TRUNC('day', users_latest_src_28000.ds) AS ds_latest__day - , DATE_TRUNC('week', users_latest_src_28000.ds) AS ds_latest__week - , DATE_TRUNC('month', users_latest_src_28000.ds) AS ds_latest__month - , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS ds_latest__quarter - , DATE_TRUNC('year', users_latest_src_28000.ds) AS ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS ds_latest__extract_day - , EXTRACT(dayofweekiso FROM users_latest_src_28000.ds) AS ds_latest__extract_dow - , EXTRACT(doy FROM users_latest_src_28000.ds) AS ds_latest__extract_doy - , users_latest_src_28000.home_state_latest - , DATE_TRUNC('day', users_latest_src_28000.ds) AS user__ds_latest__day - , DATE_TRUNC('week', users_latest_src_28000.ds) AS user__ds_latest__week - , DATE_TRUNC('month', users_latest_src_28000.ds) AS user__ds_latest__month - , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS user__ds_latest__quarter - , DATE_TRUNC('year', users_latest_src_28000.ds) AS user__ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS user__ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS user__ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS user__ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS user__ds_latest__extract_day - , EXTRACT(dayofweekiso FROM users_latest_src_28000.ds) AS user__ds_latest__extract_dow - , EXTRACT(doy FROM users_latest_src_28000.ds) AS user__ds_latest__extract_doy - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , users_latest_src_28000.user_id AS user - FROM ***************************.dim_users_latest users_latest_src_28000 - ) subq_12 - ) subq_13 - ON - subq_11.user = subq_13.user - ) subq_14 - ) subq_15 - INNER JOIN ( - -- Add column with generated UUID - SELECT - subq_17.ds__day - , subq_17.ds__week - , subq_17.ds__month - , subq_17.ds__quarter - , subq_17.ds__year - , subq_17.ds__extract_year - , subq_17.ds__extract_quarter - , subq_17.ds__extract_month - , subq_17.ds__extract_day - , subq_17.ds__extract_dow - , subq_17.ds__extract_doy - , subq_17.buy__ds__day - , subq_17.buy__ds__week - , subq_17.buy__ds__month - , subq_17.buy__ds__quarter - , subq_17.buy__ds__year - , subq_17.buy__ds__extract_year - , subq_17.buy__ds__extract_quarter - , subq_17.buy__ds__extract_month - , subq_17.buy__ds__extract_day - , subq_17.buy__ds__extract_dow - , subq_17.buy__ds__extract_doy - , subq_17.metric_time__day - , subq_17.metric_time__week - , subq_17.metric_time__month - , subq_17.metric_time__quarter - , subq_17.metric_time__year - , subq_17.metric_time__extract_year - , subq_17.metric_time__extract_quarter - , subq_17.metric_time__extract_month - , subq_17.metric_time__extract_day - , subq_17.metric_time__extract_dow - , subq_17.metric_time__extract_doy - , subq_17.user - , subq_17.session_id - , subq_17.buy__user - , subq_17.buy__session_id - , subq_17.buys - , subq_17.buyers - , UUID_STRING() AS mf_internal_uuid - FROM ( - -- Metric Time Dimension 'ds' + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_10 + ) subq_11 + LEFT OUTER JOIN ( + -- Pass Only Elements: ['home_state_latest', 'user'] SELECT - subq_16.ds__day - , subq_16.ds__week - , subq_16.ds__month - , subq_16.ds__quarter - , subq_16.ds__year - , subq_16.ds__extract_year - , subq_16.ds__extract_quarter - , subq_16.ds__extract_month - , subq_16.ds__extract_day - , subq_16.ds__extract_dow - , subq_16.ds__extract_doy - , subq_16.buy__ds__day - , subq_16.buy__ds__week - , subq_16.buy__ds__month - , subq_16.buy__ds__quarter - , subq_16.buy__ds__year - , subq_16.buy__ds__extract_year - , subq_16.buy__ds__extract_quarter - , subq_16.buy__ds__extract_month - , subq_16.buy__ds__extract_day - , subq_16.buy__ds__extract_dow - , subq_16.buy__ds__extract_doy - , subq_16.ds__day AS metric_time__day - , subq_16.ds__week AS metric_time__week - , subq_16.ds__month AS metric_time__month - , subq_16.ds__quarter AS metric_time__quarter - , subq_16.ds__year AS metric_time__year - , subq_16.ds__extract_year AS metric_time__extract_year - , subq_16.ds__extract_quarter AS metric_time__extract_quarter - , subq_16.ds__extract_month AS metric_time__extract_month - , subq_16.ds__extract_day AS metric_time__extract_day - , subq_16.ds__extract_dow AS metric_time__extract_dow - , subq_16.ds__extract_doy AS metric_time__extract_doy - , subq_16.user - , subq_16.session_id - , subq_16.buy__user - , subq_16.buy__session_id - , subq_16.buys - , subq_16.buyers + subq_12.user + , subq_12.home_state_latest FROM ( - -- Read Elements From Semantic Model 'buys_source' + -- Read Elements From Semantic Model 'users_latest' SELECT - 1 AS buys - , buys_source_src_28000.user_id AS buyers - , DATE_TRUNC('day', buys_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', buys_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', buys_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', buys_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day - , EXTRACT(dayofweekiso FROM buys_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM buys_source_src_28000.ds) AS ds__extract_doy - , DATE_TRUNC('day', buys_source_src_28000.ds) AS buy__ds__day - , DATE_TRUNC('week', buys_source_src_28000.ds) AS buy__ds__week - , DATE_TRUNC('month', buys_source_src_28000.ds) AS buy__ds__month - , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS buy__ds__quarter - , DATE_TRUNC('year', buys_source_src_28000.ds) AS buy__ds__year - , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year - , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter - , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month - , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day - , EXTRACT(dayofweekiso FROM buys_source_src_28000.ds) AS buy__ds__extract_dow - , EXTRACT(doy FROM buys_source_src_28000.ds) AS buy__ds__extract_doy - , buys_source_src_28000.user_id AS user - , buys_source_src_28000.session_id - , buys_source_src_28000.user_id AS buy__user - , buys_source_src_28000.session_id AS buy__session_id - FROM ***************************.fct_buys buys_source_src_28000 - ) subq_16 - ) subq_17 - ) subq_18 - ON - ( - subq_15.user = subq_18.user - ) AND ( - ( - subq_15.ds__day <= subq_18.ds__day - ) AND ( - subq_15.ds__day > DATEADD(day, -7, subq_18.ds__day) - ) - ) - ) subq_19 - ) subq_20 - ) subq_21 - LEFT OUTER JOIN ( - -- Pass Only Elements: ['home_state_latest', 'user'] - SELECT - subq_22.user - , subq_22.home_state_latest - FROM ( - -- Read Elements From Semantic Model 'users_latest' + DATE_TRUNC('day', users_latest_src_28000.ds) AS ds_latest__day + , DATE_TRUNC('week', users_latest_src_28000.ds) AS ds_latest__week + , DATE_TRUNC('month', users_latest_src_28000.ds) AS ds_latest__month + , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS ds_latest__quarter + , DATE_TRUNC('year', users_latest_src_28000.ds) AS ds_latest__year + , EXTRACT(year FROM users_latest_src_28000.ds) AS ds_latest__extract_year + , EXTRACT(quarter FROM users_latest_src_28000.ds) AS ds_latest__extract_quarter + , EXTRACT(month FROM users_latest_src_28000.ds) AS ds_latest__extract_month + , EXTRACT(day FROM users_latest_src_28000.ds) AS ds_latest__extract_day + , EXTRACT(dayofweekiso FROM users_latest_src_28000.ds) AS ds_latest__extract_dow + , EXTRACT(doy FROM users_latest_src_28000.ds) AS ds_latest__extract_doy + , users_latest_src_28000.home_state_latest + , DATE_TRUNC('day', users_latest_src_28000.ds) AS user__ds_latest__day + , DATE_TRUNC('week', users_latest_src_28000.ds) AS user__ds_latest__week + , DATE_TRUNC('month', users_latest_src_28000.ds) AS user__ds_latest__month + , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS user__ds_latest__quarter + , DATE_TRUNC('year', users_latest_src_28000.ds) AS user__ds_latest__year + , EXTRACT(year FROM users_latest_src_28000.ds) AS user__ds_latest__extract_year + , EXTRACT(quarter FROM users_latest_src_28000.ds) AS user__ds_latest__extract_quarter + , EXTRACT(month FROM users_latest_src_28000.ds) AS user__ds_latest__extract_month + , EXTRACT(day FROM users_latest_src_28000.ds) AS user__ds_latest__extract_day + , EXTRACT(dayofweekiso FROM users_latest_src_28000.ds) AS user__ds_latest__extract_dow + , EXTRACT(doy FROM users_latest_src_28000.ds) AS user__ds_latest__extract_doy + , users_latest_src_28000.home_state_latest AS user__home_state_latest + , users_latest_src_28000.user_id AS user + FROM ***************************.dim_users_latest users_latest_src_28000 + ) subq_12 + ) subq_13 + ON + subq_11.user = subq_13.user + ) subq_14 + WHERE visit__referrer_id = '123456' + ) subq_15 + ) subq_16 + INNER JOIN ( + -- Add column with generated UUID SELECT - DATE_TRUNC('day', users_latest_src_28000.ds) AS ds_latest__day - , DATE_TRUNC('week', users_latest_src_28000.ds) AS ds_latest__week - , DATE_TRUNC('month', users_latest_src_28000.ds) AS ds_latest__month - , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS ds_latest__quarter - , DATE_TRUNC('year', users_latest_src_28000.ds) AS ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS ds_latest__extract_day - , EXTRACT(dayofweekiso FROM users_latest_src_28000.ds) AS ds_latest__extract_dow - , EXTRACT(doy FROM users_latest_src_28000.ds) AS ds_latest__extract_doy - , users_latest_src_28000.home_state_latest - , DATE_TRUNC('day', users_latest_src_28000.ds) AS user__ds_latest__day - , DATE_TRUNC('week', users_latest_src_28000.ds) AS user__ds_latest__week - , DATE_TRUNC('month', users_latest_src_28000.ds) AS user__ds_latest__month - , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS user__ds_latest__quarter - , DATE_TRUNC('year', users_latest_src_28000.ds) AS user__ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS user__ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS user__ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS user__ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS user__ds_latest__extract_day - , EXTRACT(dayofweekiso FROM users_latest_src_28000.ds) AS user__ds_latest__extract_dow - , EXTRACT(doy FROM users_latest_src_28000.ds) AS user__ds_latest__extract_doy - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , users_latest_src_28000.user_id AS user - FROM ***************************.dim_users_latest users_latest_src_28000 - ) subq_22 - ) subq_23 - ON - subq_21.user = subq_23.user - ) subq_24 - ) subq_25 + subq_18.ds__day + , subq_18.ds__week + , subq_18.ds__month + , subq_18.ds__quarter + , subq_18.ds__year + , subq_18.ds__extract_year + , subq_18.ds__extract_quarter + , subq_18.ds__extract_month + , subq_18.ds__extract_day + , subq_18.ds__extract_dow + , subq_18.ds__extract_doy + , subq_18.buy__ds__day + , subq_18.buy__ds__week + , subq_18.buy__ds__month + , subq_18.buy__ds__quarter + , subq_18.buy__ds__year + , subq_18.buy__ds__extract_year + , subq_18.buy__ds__extract_quarter + , subq_18.buy__ds__extract_month + , subq_18.buy__ds__extract_day + , subq_18.buy__ds__extract_dow + , subq_18.buy__ds__extract_doy + , subq_18.metric_time__day + , subq_18.metric_time__week + , subq_18.metric_time__month + , subq_18.metric_time__quarter + , subq_18.metric_time__year + , subq_18.metric_time__extract_year + , subq_18.metric_time__extract_quarter + , subq_18.metric_time__extract_month + , subq_18.metric_time__extract_day + , subq_18.metric_time__extract_dow + , subq_18.metric_time__extract_doy + , subq_18.user + , subq_18.session_id + , subq_18.buy__user + , subq_18.buy__session_id + , subq_18.buys + , subq_18.buyers + , UUID_STRING() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_17.ds__day + , subq_17.ds__week + , subq_17.ds__month + , subq_17.ds__quarter + , subq_17.ds__year + , subq_17.ds__extract_year + , subq_17.ds__extract_quarter + , subq_17.ds__extract_month + , subq_17.ds__extract_day + , subq_17.ds__extract_dow + , subq_17.ds__extract_doy + , subq_17.buy__ds__day + , subq_17.buy__ds__week + , subq_17.buy__ds__month + , subq_17.buy__ds__quarter + , subq_17.buy__ds__year + , subq_17.buy__ds__extract_year + , subq_17.buy__ds__extract_quarter + , subq_17.buy__ds__extract_month + , subq_17.buy__ds__extract_day + , subq_17.buy__ds__extract_dow + , subq_17.buy__ds__extract_doy + , subq_17.ds__day AS metric_time__day + , subq_17.ds__week AS metric_time__week + , subq_17.ds__month AS metric_time__month + , subq_17.ds__quarter AS metric_time__quarter + , subq_17.ds__year AS metric_time__year + , subq_17.ds__extract_year AS metric_time__extract_year + , subq_17.ds__extract_quarter AS metric_time__extract_quarter + , subq_17.ds__extract_month AS metric_time__extract_month + , subq_17.ds__extract_day AS metric_time__extract_day + , subq_17.ds__extract_dow AS metric_time__extract_dow + , subq_17.ds__extract_doy AS metric_time__extract_doy + , subq_17.user + , subq_17.session_id + , subq_17.buy__user + , subq_17.buy__session_id + , subq_17.buys + , subq_17.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_28000.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM buys_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_28000.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day + , EXTRACT(dayofweekiso FROM buys_source_src_28000.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS buy__ds__extract_doy + , buys_source_src_28000.user_id AS user + , buys_source_src_28000.session_id + , buys_source_src_28000.user_id AS buy__user + , buys_source_src_28000.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_17 + ) subq_18 + ) subq_19 + ON + ( + subq_16.user = subq_19.user + ) AND ( + ( + subq_16.ds__day <= subq_19.ds__day + ) AND ( + subq_16.ds__day > DATEADD(day, -7, subq_19.ds__day) + ) + ) + ) subq_20 + ) subq_21 + ) subq_22 GROUP BY - subq_25.metric_time__day - , subq_25.user__home_state_latest - ) subq_26 + subq_22.metric_time__day + , subq_22.user__home_state_latest + ) subq_23 ON ( - subq_9.user__home_state_latest = subq_26.user__home_state_latest + subq_9.user__home_state_latest = subq_23.user__home_state_latest ) AND ( - subq_9.metric_time__day = subq_26.metric_time__day + subq_9.metric_time__day = subq_23.metric_time__day ) GROUP BY - COALESCE(subq_9.metric_time__day, subq_26.metric_time__day) - , COALESCE(subq_9.user__home_state_latest, subq_26.user__home_state_latest) -) subq_27 + COALESCE(subq_9.metric_time__day, subq_23.metric_time__day) + , COALESCE(subq_9.user__home_state_latest, subq_23.user__home_state_latest) +) subq_24 diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_query_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_query_filters__plan0_optimized.sql index 78e9355b9a..b34a22c481 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_query_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_conversion_metric_query_filters__plan0_optimized.sql @@ -6,10 +6,10 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_37.metric_time__day, subq_54.metric_time__day) AS metric_time__day - , COALESCE(subq_37.user__home_state_latest, subq_54.user__home_state_latest) AS user__home_state_latest - , MAX(subq_37.visits) AS visits - , MAX(subq_54.buys) AS buys + COALESCE(subq_34.metric_time__day, subq_48.metric_time__day) AS metric_time__day + , COALESCE(subq_34.user__home_state_latest, subq_48.user__home_state_latest) AS user__home_state_latest + , MAX(subq_34.visits) AS visits + , MAX(subq_48.buys) AS buys FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'user__home_state_latest', 'metric_time__day'] @@ -22,10 +22,10 @@ FROM ( -- Join Standard Outputs -- Pass Only Elements: ['visits', 'user__home_state_latest', 'visit__referrer_id', 'metric_time__day'] SELECT - subq_30.metric_time__day AS metric_time__day - , subq_30.visit__referrer_id AS visit__referrer_id + subq_27.metric_time__day AS metric_time__day + , subq_27.visit__referrer_id AS visit__referrer_id , users_latest_src_28000.home_state_latest AS user__home_state_latest - , subq_30.visits AS visits + , subq_27.visits AS visits FROM ( -- Read Elements From Semantic Model 'visits_source' -- Metric Time Dimension 'ds' @@ -36,104 +36,115 @@ FROM ( , referrer_id AS visit__referrer_id , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_30 + ) subq_27 LEFT OUTER JOIN ***************************.dim_users_latest users_latest_src_28000 ON - subq_30.user = users_latest_src_28000.user_id - ) subq_34 + subq_27.user = users_latest_src_28000.user_id + ) subq_31 WHERE visit__referrer_id = '123456' GROUP BY metric_time__day , user__home_state_latest - ) subq_37 + ) subq_34 FULL OUTER JOIN ( - -- Join Standard Outputs + -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'user__home_state_latest', 'metric_time__day'] -- Aggregate Measures SELECT - subq_47.metric_time__day AS metric_time__day - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , SUM(subq_47.buys) AS buys + metric_time__day + , user__home_state_latest + , SUM(buys) AS buys FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_43.visits) OVER ( + FIRST_VALUE(subq_41.visits) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_43.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_41.visit__referrer_id) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_43.user__home_state_latest) OVER ( + , FIRST_VALUE(subq_41.user__home_state_latest) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user__home_state_latest - , FIRST_VALUE(subq_43.ds__day) OVER ( + , FIRST_VALUE(subq_41.ds__day) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_43.metric_time__day) OVER ( + , FIRST_VALUE(subq_41.metric_time__day) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_43.user) OVER ( + , FIRST_VALUE(subq_41.user) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_46.mf_internal_uuid AS mf_internal_uuid - , subq_46.buys AS buys + , subq_44.mf_internal_uuid AS mf_internal_uuid + , subq_44.buys AS buys FROM ( - -- Join Standard Outputs + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'user__home_state_latest', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_39.ds__day AS ds__day - , subq_39.metric_time__day AS metric_time__day - , subq_39.user AS user - , subq_39.visit__referrer_id AS visit__referrer_id - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , subq_39.visits AS visits + ds__day + , metric_time__day + , subq_39.user + , visit__referrer_id + , user__home_state_latest + , visits FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 + subq_36.ds__day AS ds__day + , subq_36.metric_time__day AS metric_time__day + , subq_36.user AS user + , subq_36.visit__referrer_id AS visit__referrer_id + , users_latest_src_28000.home_state_latest AS user__home_state_latest + , subq_36.visits AS visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_36 + LEFT OUTER JOIN + ***************************.dim_users_latest users_latest_src_28000 + ON + subq_36.user = users_latest_src_28000.user_id ) subq_39 - LEFT OUTER JOIN - ***************************.dim_users_latest users_latest_src_28000 - ON - subq_39.user = users_latest_src_28000.user_id - ) subq_43 + WHERE visit__referrer_id = '123456' + ) subq_41 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -144,33 +155,29 @@ FROM ( , 1 AS buys , UUID_STRING() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_46 + ) subq_44 ON ( - subq_43.user = subq_46.user + subq_41.user = subq_44.user ) AND ( ( - subq_43.ds__day <= subq_46.ds__day + subq_41.ds__day <= subq_44.ds__day ) AND ( - subq_43.ds__day > DATEADD(day, -7, subq_46.ds__day) + subq_41.ds__day > DATEADD(day, -7, subq_44.ds__day) ) ) - ) subq_47 - LEFT OUTER JOIN - ***************************.dim_users_latest users_latest_src_28000 - ON - subq_47.user = users_latest_src_28000.user_id + ) subq_45 GROUP BY - subq_47.metric_time__day - , users_latest_src_28000.home_state_latest - ) subq_54 + metric_time__day + , user__home_state_latest + ) subq_48 ON ( - subq_37.user__home_state_latest = subq_54.user__home_state_latest + subq_34.user__home_state_latest = subq_48.user__home_state_latest ) AND ( - subq_37.metric_time__day = subq_54.metric_time__day + subq_34.metric_time__day = subq_48.metric_time__day ) GROUP BY - COALESCE(subq_37.metric_time__day, subq_54.metric_time__day) - , COALESCE(subq_37.user__home_state_latest, subq_54.user__home_state_latest) -) subq_55 + COALESCE(subq_34.metric_time__day, subq_48.metric_time__day) + , COALESCE(subq_34.user__home_state_latest, subq_48.user__home_state_latest) +) subq_49 diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_query_filters__plan0.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_query_filters__plan0.sql index 0c0574cb54..3e2fb47c41 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_query_filters__plan0.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_query_filters__plan0.sql @@ -1,15 +1,15 @@ -- Compute Metrics via Expressions SELECT - subq_27.metric_time__day - , subq_27.user__home_state_latest - , CAST(subq_27.buys AS DOUBLE) / CAST(NULLIF(subq_27.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days + subq_24.metric_time__day + , subq_24.user__home_state_latest + , CAST(subq_24.buys AS DOUBLE) / CAST(NULLIF(subq_24.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_9.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , COALESCE(subq_9.user__home_state_latest, subq_26.user__home_state_latest) AS user__home_state_latest + COALESCE(subq_9.metric_time__day, subq_23.metric_time__day) AS metric_time__day + , COALESCE(subq_9.user__home_state_latest, subq_23.user__home_state_latest) AS user__home_state_latest , MAX(subq_9.visits) AS visits - , MAX(subq_26.buys) AS buys + , MAX(subq_23.buys) AS buys FROM ( -- Aggregate Measures SELECT @@ -182,451 +182,443 @@ FROM ( FULL OUTER JOIN ( -- Aggregate Measures SELECT - subq_25.metric_time__day - , subq_25.user__home_state_latest - , SUM(subq_25.buys) AS buys + subq_22.metric_time__day + , subq_22.user__home_state_latest + , SUM(subq_22.buys) AS buys FROM ( -- Pass Only Elements: ['buys', 'user__home_state_latest', 'metric_time__day'] SELECT - subq_24.metric_time__day - , subq_24.user__home_state_latest - , subq_24.buys + subq_21.metric_time__day + , subq_21.user__home_state_latest + , subq_21.buys FROM ( - -- Join Standard Outputs + -- Find conversions for user within the range of 7 day SELECT - subq_21.metric_time__day AS metric_time__day - , subq_21.user AS user - , subq_21.visit__referrer_id AS visit__referrer_id - , subq_23.home_state_latest AS user__home_state_latest - , subq_21.buys AS buys + subq_20.ds__day + , subq_20.metric_time__day + , subq_20.user + , subq_20.visit__referrer_id + , subq_20.user__home_state_latest + , subq_20.buys + , subq_20.visits FROM ( - -- Pass Only Elements: ['buys', 'visit__referrer_id', 'metric_time__day', 'user'] - SELECT - subq_20.metric_time__day - , subq_20.user - , subq_20.visit__referrer_id - , subq_20.buys + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + FIRST_VALUE(subq_16.visits) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , FIRST_VALUE(subq_16.visit__referrer_id) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visit__referrer_id + , FIRST_VALUE(subq_16.user__home_state_latest) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user__home_state_latest + , FIRST_VALUE(subq_16.ds__day) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , FIRST_VALUE(subq_16.metric_time__day) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS metric_time__day + , FIRST_VALUE(subq_16.user) OVER ( + PARTITION BY + subq_19.user + , subq_19.ds__day + , subq_19.mf_internal_uuid + ORDER BY subq_16.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_19.mf_internal_uuid AS mf_internal_uuid + , subq_19.buys AS buys FROM ( - -- Find conversions for user within the range of 7 day + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'user__home_state_latest', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_19.ds__day - , subq_19.metric_time__day - , subq_19.user - , subq_19.visit__referrer_id - , subq_19.user__home_state_latest - , subq_19.buys - , subq_19.visits + subq_15.ds__day + , subq_15.metric_time__day + , subq_15.user + , subq_15.visit__referrer_id + , subq_15.user__home_state_latest + , subq_15.visits FROM ( - -- Dedupe the fanout with mf_internal_uuid in the conversion data set - SELECT DISTINCT - FIRST_VALUE(subq_15.visits) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS visits - , FIRST_VALUE(subq_15.visit__referrer_id) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS visit__referrer_id - , FIRST_VALUE(subq_15.user__home_state_latest) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS user__home_state_latest - , FIRST_VALUE(subq_15.ds__day) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS ds__day - , FIRST_VALUE(subq_15.metric_time__day) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS metric_time__day - , FIRST_VALUE(subq_15.user) OVER ( - PARTITION BY - subq_18.user - , subq_18.ds__day - , subq_18.mf_internal_uuid - ORDER BY subq_15.ds__day DESC - ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING - ) AS user - , subq_18.mf_internal_uuid AS mf_internal_uuid - , subq_18.buys AS buys + -- Constrain Output with WHERE + SELECT + subq_14.ds__day + , subq_14.ds__week + , subq_14.ds__month + , subq_14.ds__quarter + , subq_14.ds__year + , subq_14.ds__extract_year + , subq_14.ds__extract_quarter + , subq_14.ds__extract_month + , subq_14.ds__extract_day + , subq_14.ds__extract_dow + , subq_14.ds__extract_doy + , subq_14.visit__ds__day + , subq_14.visit__ds__week + , subq_14.visit__ds__month + , subq_14.visit__ds__quarter + , subq_14.visit__ds__year + , subq_14.visit__ds__extract_year + , subq_14.visit__ds__extract_quarter + , subq_14.visit__ds__extract_month + , subq_14.visit__ds__extract_day + , subq_14.visit__ds__extract_dow + , subq_14.visit__ds__extract_doy + , subq_14.metric_time__day + , subq_14.metric_time__week + , subq_14.metric_time__month + , subq_14.metric_time__quarter + , subq_14.metric_time__year + , subq_14.metric_time__extract_year + , subq_14.metric_time__extract_quarter + , subq_14.metric_time__extract_month + , subq_14.metric_time__extract_day + , subq_14.metric_time__extract_dow + , subq_14.metric_time__extract_doy + , subq_14.user + , subq_14.session + , subq_14.visit__user + , subq_14.visit__session + , subq_14.referrer_id + , subq_14.visit__referrer_id + , subq_14.user__home_state_latest + , subq_14.visits + , subq_14.visitors FROM ( - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'user__home_state_latest', 'ds__day', 'metric_time__day', 'user'] + -- Join Standard Outputs SELECT - subq_14.ds__day - , subq_14.metric_time__day - , subq_14.user - , subq_14.visit__referrer_id - , subq_14.user__home_state_latest - , subq_14.visits + subq_11.ds__day AS ds__day + , subq_11.ds__week AS ds__week + , subq_11.ds__month AS ds__month + , subq_11.ds__quarter AS ds__quarter + , subq_11.ds__year AS ds__year + , subq_11.ds__extract_year AS ds__extract_year + , subq_11.ds__extract_quarter AS ds__extract_quarter + , subq_11.ds__extract_month AS ds__extract_month + , subq_11.ds__extract_day AS ds__extract_day + , subq_11.ds__extract_dow AS ds__extract_dow + , subq_11.ds__extract_doy AS ds__extract_doy + , subq_11.visit__ds__day AS visit__ds__day + , subq_11.visit__ds__week AS visit__ds__week + , subq_11.visit__ds__month AS visit__ds__month + , subq_11.visit__ds__quarter AS visit__ds__quarter + , subq_11.visit__ds__year AS visit__ds__year + , subq_11.visit__ds__extract_year AS visit__ds__extract_year + , subq_11.visit__ds__extract_quarter AS visit__ds__extract_quarter + , subq_11.visit__ds__extract_month AS visit__ds__extract_month + , subq_11.visit__ds__extract_day AS visit__ds__extract_day + , subq_11.visit__ds__extract_dow AS visit__ds__extract_dow + , subq_11.visit__ds__extract_doy AS visit__ds__extract_doy + , subq_11.metric_time__day AS metric_time__day + , subq_11.metric_time__week AS metric_time__week + , subq_11.metric_time__month AS metric_time__month + , subq_11.metric_time__quarter AS metric_time__quarter + , subq_11.metric_time__year AS metric_time__year + , subq_11.metric_time__extract_year AS metric_time__extract_year + , subq_11.metric_time__extract_quarter AS metric_time__extract_quarter + , subq_11.metric_time__extract_month AS metric_time__extract_month + , subq_11.metric_time__extract_day AS metric_time__extract_day + , subq_11.metric_time__extract_dow AS metric_time__extract_dow + , subq_11.metric_time__extract_doy AS metric_time__extract_doy + , subq_11.user AS user + , subq_11.session AS session + , subq_11.visit__user AS visit__user + , subq_11.visit__session AS visit__session + , subq_11.referrer_id AS referrer_id + , subq_11.visit__referrer_id AS visit__referrer_id + , subq_13.home_state_latest AS user__home_state_latest + , subq_11.visits AS visits + , subq_11.visitors AS visitors FROM ( - -- Join Standard Outputs + -- Metric Time Dimension 'ds' SELECT - subq_11.ds__day AS ds__day - , subq_11.ds__week AS ds__week - , subq_11.ds__month AS ds__month - , subq_11.ds__quarter AS ds__quarter - , subq_11.ds__year AS ds__year - , subq_11.ds__extract_year AS ds__extract_year - , subq_11.ds__extract_quarter AS ds__extract_quarter - , subq_11.ds__extract_month AS ds__extract_month - , subq_11.ds__extract_day AS ds__extract_day - , subq_11.ds__extract_dow AS ds__extract_dow - , subq_11.ds__extract_doy AS ds__extract_doy - , subq_11.visit__ds__day AS visit__ds__day - , subq_11.visit__ds__week AS visit__ds__week - , subq_11.visit__ds__month AS visit__ds__month - , subq_11.visit__ds__quarter AS visit__ds__quarter - , subq_11.visit__ds__year AS visit__ds__year - , subq_11.visit__ds__extract_year AS visit__ds__extract_year - , subq_11.visit__ds__extract_quarter AS visit__ds__extract_quarter - , subq_11.visit__ds__extract_month AS visit__ds__extract_month - , subq_11.visit__ds__extract_day AS visit__ds__extract_day - , subq_11.visit__ds__extract_dow AS visit__ds__extract_dow - , subq_11.visit__ds__extract_doy AS visit__ds__extract_doy - , subq_11.metric_time__day AS metric_time__day - , subq_11.metric_time__week AS metric_time__week - , subq_11.metric_time__month AS metric_time__month - , subq_11.metric_time__quarter AS metric_time__quarter - , subq_11.metric_time__year AS metric_time__year - , subq_11.metric_time__extract_year AS metric_time__extract_year - , subq_11.metric_time__extract_quarter AS metric_time__extract_quarter - , subq_11.metric_time__extract_month AS metric_time__extract_month - , subq_11.metric_time__extract_day AS metric_time__extract_day - , subq_11.metric_time__extract_dow AS metric_time__extract_dow - , subq_11.metric_time__extract_doy AS metric_time__extract_doy - , subq_11.user AS user - , subq_11.session AS session - , subq_11.visit__user AS visit__user - , subq_11.visit__session AS visit__session - , subq_11.referrer_id AS referrer_id - , subq_11.visit__referrer_id AS visit__referrer_id - , subq_13.home_state_latest AS user__home_state_latest - , subq_11.visits AS visits - , subq_11.visitors AS visitors + subq_10.ds__day + , subq_10.ds__week + , subq_10.ds__month + , subq_10.ds__quarter + , subq_10.ds__year + , subq_10.ds__extract_year + , subq_10.ds__extract_quarter + , subq_10.ds__extract_month + , subq_10.ds__extract_day + , subq_10.ds__extract_dow + , subq_10.ds__extract_doy + , subq_10.visit__ds__day + , subq_10.visit__ds__week + , subq_10.visit__ds__month + , subq_10.visit__ds__quarter + , subq_10.visit__ds__year + , subq_10.visit__ds__extract_year + , subq_10.visit__ds__extract_quarter + , subq_10.visit__ds__extract_month + , subq_10.visit__ds__extract_day + , subq_10.visit__ds__extract_dow + , subq_10.visit__ds__extract_doy + , subq_10.ds__day AS metric_time__day + , subq_10.ds__week AS metric_time__week + , subq_10.ds__month AS metric_time__month + , subq_10.ds__quarter AS metric_time__quarter + , subq_10.ds__year AS metric_time__year + , subq_10.ds__extract_year AS metric_time__extract_year + , subq_10.ds__extract_quarter AS metric_time__extract_quarter + , subq_10.ds__extract_month AS metric_time__extract_month + , subq_10.ds__extract_day AS metric_time__extract_day + , subq_10.ds__extract_dow AS metric_time__extract_dow + , subq_10.ds__extract_doy AS metric_time__extract_doy + , subq_10.user + , subq_10.session + , subq_10.visit__user + , subq_10.visit__session + , subq_10.referrer_id + , subq_10.visit__referrer_id + , subq_10.visits + , subq_10.visitors FROM ( - -- Metric Time Dimension 'ds' - SELECT - subq_10.ds__day - , subq_10.ds__week - , subq_10.ds__month - , subq_10.ds__quarter - , subq_10.ds__year - , subq_10.ds__extract_year - , subq_10.ds__extract_quarter - , subq_10.ds__extract_month - , subq_10.ds__extract_day - , subq_10.ds__extract_dow - , subq_10.ds__extract_doy - , subq_10.visit__ds__day - , subq_10.visit__ds__week - , subq_10.visit__ds__month - , subq_10.visit__ds__quarter - , subq_10.visit__ds__year - , subq_10.visit__ds__extract_year - , subq_10.visit__ds__extract_quarter - , subq_10.visit__ds__extract_month - , subq_10.visit__ds__extract_day - , subq_10.visit__ds__extract_dow - , subq_10.visit__ds__extract_doy - , subq_10.ds__day AS metric_time__day - , subq_10.ds__week AS metric_time__week - , subq_10.ds__month AS metric_time__month - , subq_10.ds__quarter AS metric_time__quarter - , subq_10.ds__year AS metric_time__year - , subq_10.ds__extract_year AS metric_time__extract_year - , subq_10.ds__extract_quarter AS metric_time__extract_quarter - , subq_10.ds__extract_month AS metric_time__extract_month - , subq_10.ds__extract_day AS metric_time__extract_day - , subq_10.ds__extract_dow AS metric_time__extract_dow - , subq_10.ds__extract_doy AS metric_time__extract_doy - , subq_10.user - , subq_10.session - , subq_10.visit__user - , subq_10.visit__session - , subq_10.referrer_id - , subq_10.visit__referrer_id - , subq_10.visits - , subq_10.visitors - FROM ( - -- Read Elements From Semantic Model 'visits_source' - SELECT - 1 AS visits - , visits_source_src_28000.user_id AS visitors - , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day - , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy - , visits_source_src_28000.referrer_id - , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day - , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week - , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month - , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter - , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year - , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year - , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter - , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month - , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day - , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS visit__ds__extract_dow - , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy - , visits_source_src_28000.referrer_id AS visit__referrer_id - , visits_source_src_28000.user_id AS user - , visits_source_src_28000.session_id AS session - , visits_source_src_28000.user_id AS visit__user - , visits_source_src_28000.session_id AS visit__session - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_10 - ) subq_11 - LEFT OUTER JOIN ( - -- Pass Only Elements: ['home_state_latest', 'user'] + -- Read Elements From Semantic Model 'visits_source' SELECT - subq_12.user - , subq_12.home_state_latest - FROM ( - -- Read Elements From Semantic Model 'users_latest' - SELECT - DATE_TRUNC('day', users_latest_src_28000.ds) AS ds_latest__day - , DATE_TRUNC('week', users_latest_src_28000.ds) AS ds_latest__week - , DATE_TRUNC('month', users_latest_src_28000.ds) AS ds_latest__month - , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS ds_latest__quarter - , DATE_TRUNC('year', users_latest_src_28000.ds) AS ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS ds_latest__extract_day - , EXTRACT(DAY_OF_WEEK FROM users_latest_src_28000.ds) AS ds_latest__extract_dow - , EXTRACT(doy FROM users_latest_src_28000.ds) AS ds_latest__extract_doy - , users_latest_src_28000.home_state_latest - , DATE_TRUNC('day', users_latest_src_28000.ds) AS user__ds_latest__day - , DATE_TRUNC('week', users_latest_src_28000.ds) AS user__ds_latest__week - , DATE_TRUNC('month', users_latest_src_28000.ds) AS user__ds_latest__month - , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS user__ds_latest__quarter - , DATE_TRUNC('year', users_latest_src_28000.ds) AS user__ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS user__ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS user__ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS user__ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS user__ds_latest__extract_day - , EXTRACT(DAY_OF_WEEK FROM users_latest_src_28000.ds) AS user__ds_latest__extract_dow - , EXTRACT(doy FROM users_latest_src_28000.ds) AS user__ds_latest__extract_doy - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , users_latest_src_28000.user_id AS user - FROM ***************************.dim_users_latest users_latest_src_28000 - ) subq_12 - ) subq_13 - ON - subq_11.user = subq_13.user - ) subq_14 - ) subq_15 - INNER JOIN ( - -- Add column with generated UUID - SELECT - subq_17.ds__day - , subq_17.ds__week - , subq_17.ds__month - , subq_17.ds__quarter - , subq_17.ds__year - , subq_17.ds__extract_year - , subq_17.ds__extract_quarter - , subq_17.ds__extract_month - , subq_17.ds__extract_day - , subq_17.ds__extract_dow - , subq_17.ds__extract_doy - , subq_17.buy__ds__day - , subq_17.buy__ds__week - , subq_17.buy__ds__month - , subq_17.buy__ds__quarter - , subq_17.buy__ds__year - , subq_17.buy__ds__extract_year - , subq_17.buy__ds__extract_quarter - , subq_17.buy__ds__extract_month - , subq_17.buy__ds__extract_day - , subq_17.buy__ds__extract_dow - , subq_17.buy__ds__extract_doy - , subq_17.metric_time__day - , subq_17.metric_time__week - , subq_17.metric_time__month - , subq_17.metric_time__quarter - , subq_17.metric_time__year - , subq_17.metric_time__extract_year - , subq_17.metric_time__extract_quarter - , subq_17.metric_time__extract_month - , subq_17.metric_time__extract_day - , subq_17.metric_time__extract_dow - , subq_17.metric_time__extract_doy - , subq_17.user - , subq_17.session_id - , subq_17.buy__user - , subq_17.buy__session_id - , subq_17.buys - , subq_17.buyers - , uuid() AS mf_internal_uuid - FROM ( - -- Metric Time Dimension 'ds' + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS ds__extract_doy + , visits_source_src_28000.referrer_id + , DATE_TRUNC('day', visits_source_src_28000.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_28000.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_28000.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_28000.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_28000.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_28000.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_28000.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_28000.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_28000.ds) AS visit__ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM visits_source_src_28000.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_28000.ds) AS visit__ds__extract_doy + , visits_source_src_28000.referrer_id AS visit__referrer_id + , visits_source_src_28000.user_id AS user + , visits_source_src_28000.session_id AS session + , visits_source_src_28000.user_id AS visit__user + , visits_source_src_28000.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_10 + ) subq_11 + LEFT OUTER JOIN ( + -- Pass Only Elements: ['home_state_latest', 'user'] SELECT - subq_16.ds__day - , subq_16.ds__week - , subq_16.ds__month - , subq_16.ds__quarter - , subq_16.ds__year - , subq_16.ds__extract_year - , subq_16.ds__extract_quarter - , subq_16.ds__extract_month - , subq_16.ds__extract_day - , subq_16.ds__extract_dow - , subq_16.ds__extract_doy - , subq_16.buy__ds__day - , subq_16.buy__ds__week - , subq_16.buy__ds__month - , subq_16.buy__ds__quarter - , subq_16.buy__ds__year - , subq_16.buy__ds__extract_year - , subq_16.buy__ds__extract_quarter - , subq_16.buy__ds__extract_month - , subq_16.buy__ds__extract_day - , subq_16.buy__ds__extract_dow - , subq_16.buy__ds__extract_doy - , subq_16.ds__day AS metric_time__day - , subq_16.ds__week AS metric_time__week - , subq_16.ds__month AS metric_time__month - , subq_16.ds__quarter AS metric_time__quarter - , subq_16.ds__year AS metric_time__year - , subq_16.ds__extract_year AS metric_time__extract_year - , subq_16.ds__extract_quarter AS metric_time__extract_quarter - , subq_16.ds__extract_month AS metric_time__extract_month - , subq_16.ds__extract_day AS metric_time__extract_day - , subq_16.ds__extract_dow AS metric_time__extract_dow - , subq_16.ds__extract_doy AS metric_time__extract_doy - , subq_16.user - , subq_16.session_id - , subq_16.buy__user - , subq_16.buy__session_id - , subq_16.buys - , subq_16.buyers + subq_12.user + , subq_12.home_state_latest FROM ( - -- Read Elements From Semantic Model 'buys_source' + -- Read Elements From Semantic Model 'users_latest' SELECT - 1 AS buys - , buys_source_src_28000.user_id AS buyers - , DATE_TRUNC('day', buys_source_src_28000.ds) AS ds__day - , DATE_TRUNC('week', buys_source_src_28000.ds) AS ds__week - , DATE_TRUNC('month', buys_source_src_28000.ds) AS ds__month - , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS ds__quarter - , DATE_TRUNC('year', buys_source_src_28000.ds) AS ds__year - , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year - , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter - , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month - , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day - , EXTRACT(DAY_OF_WEEK FROM buys_source_src_28000.ds) AS ds__extract_dow - , EXTRACT(doy FROM buys_source_src_28000.ds) AS ds__extract_doy - , DATE_TRUNC('day', buys_source_src_28000.ds) AS buy__ds__day - , DATE_TRUNC('week', buys_source_src_28000.ds) AS buy__ds__week - , DATE_TRUNC('month', buys_source_src_28000.ds) AS buy__ds__month - , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS buy__ds__quarter - , DATE_TRUNC('year', buys_source_src_28000.ds) AS buy__ds__year - , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year - , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter - , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month - , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day - , EXTRACT(DAY_OF_WEEK FROM buys_source_src_28000.ds) AS buy__ds__extract_dow - , EXTRACT(doy FROM buys_source_src_28000.ds) AS buy__ds__extract_doy - , buys_source_src_28000.user_id AS user - , buys_source_src_28000.session_id - , buys_source_src_28000.user_id AS buy__user - , buys_source_src_28000.session_id AS buy__session_id - FROM ***************************.fct_buys buys_source_src_28000 - ) subq_16 - ) subq_17 - ) subq_18 - ON - ( - subq_15.user = subq_18.user - ) AND ( - ( - subq_15.ds__day <= subq_18.ds__day - ) AND ( - subq_15.ds__day > DATE_ADD('day', -7, subq_18.ds__day) - ) - ) - ) subq_19 - ) subq_20 - ) subq_21 - LEFT OUTER JOIN ( - -- Pass Only Elements: ['home_state_latest', 'user'] - SELECT - subq_22.user - , subq_22.home_state_latest - FROM ( - -- Read Elements From Semantic Model 'users_latest' + DATE_TRUNC('day', users_latest_src_28000.ds) AS ds_latest__day + , DATE_TRUNC('week', users_latest_src_28000.ds) AS ds_latest__week + , DATE_TRUNC('month', users_latest_src_28000.ds) AS ds_latest__month + , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS ds_latest__quarter + , DATE_TRUNC('year', users_latest_src_28000.ds) AS ds_latest__year + , EXTRACT(year FROM users_latest_src_28000.ds) AS ds_latest__extract_year + , EXTRACT(quarter FROM users_latest_src_28000.ds) AS ds_latest__extract_quarter + , EXTRACT(month FROM users_latest_src_28000.ds) AS ds_latest__extract_month + , EXTRACT(day FROM users_latest_src_28000.ds) AS ds_latest__extract_day + , EXTRACT(DAY_OF_WEEK FROM users_latest_src_28000.ds) AS ds_latest__extract_dow + , EXTRACT(doy FROM users_latest_src_28000.ds) AS ds_latest__extract_doy + , users_latest_src_28000.home_state_latest + , DATE_TRUNC('day', users_latest_src_28000.ds) AS user__ds_latest__day + , DATE_TRUNC('week', users_latest_src_28000.ds) AS user__ds_latest__week + , DATE_TRUNC('month', users_latest_src_28000.ds) AS user__ds_latest__month + , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS user__ds_latest__quarter + , DATE_TRUNC('year', users_latest_src_28000.ds) AS user__ds_latest__year + , EXTRACT(year FROM users_latest_src_28000.ds) AS user__ds_latest__extract_year + , EXTRACT(quarter FROM users_latest_src_28000.ds) AS user__ds_latest__extract_quarter + , EXTRACT(month FROM users_latest_src_28000.ds) AS user__ds_latest__extract_month + , EXTRACT(day FROM users_latest_src_28000.ds) AS user__ds_latest__extract_day + , EXTRACT(DAY_OF_WEEK FROM users_latest_src_28000.ds) AS user__ds_latest__extract_dow + , EXTRACT(doy FROM users_latest_src_28000.ds) AS user__ds_latest__extract_doy + , users_latest_src_28000.home_state_latest AS user__home_state_latest + , users_latest_src_28000.user_id AS user + FROM ***************************.dim_users_latest users_latest_src_28000 + ) subq_12 + ) subq_13 + ON + subq_11.user = subq_13.user + ) subq_14 + WHERE visit__referrer_id = '123456' + ) subq_15 + ) subq_16 + INNER JOIN ( + -- Add column with generated UUID SELECT - DATE_TRUNC('day', users_latest_src_28000.ds) AS ds_latest__day - , DATE_TRUNC('week', users_latest_src_28000.ds) AS ds_latest__week - , DATE_TRUNC('month', users_latest_src_28000.ds) AS ds_latest__month - , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS ds_latest__quarter - , DATE_TRUNC('year', users_latest_src_28000.ds) AS ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS ds_latest__extract_day - , EXTRACT(DAY_OF_WEEK FROM users_latest_src_28000.ds) AS ds_latest__extract_dow - , EXTRACT(doy FROM users_latest_src_28000.ds) AS ds_latest__extract_doy - , users_latest_src_28000.home_state_latest - , DATE_TRUNC('day', users_latest_src_28000.ds) AS user__ds_latest__day - , DATE_TRUNC('week', users_latest_src_28000.ds) AS user__ds_latest__week - , DATE_TRUNC('month', users_latest_src_28000.ds) AS user__ds_latest__month - , DATE_TRUNC('quarter', users_latest_src_28000.ds) AS user__ds_latest__quarter - , DATE_TRUNC('year', users_latest_src_28000.ds) AS user__ds_latest__year - , EXTRACT(year FROM users_latest_src_28000.ds) AS user__ds_latest__extract_year - , EXTRACT(quarter FROM users_latest_src_28000.ds) AS user__ds_latest__extract_quarter - , EXTRACT(month FROM users_latest_src_28000.ds) AS user__ds_latest__extract_month - , EXTRACT(day FROM users_latest_src_28000.ds) AS user__ds_latest__extract_day - , EXTRACT(DAY_OF_WEEK FROM users_latest_src_28000.ds) AS user__ds_latest__extract_dow - , EXTRACT(doy FROM users_latest_src_28000.ds) AS user__ds_latest__extract_doy - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , users_latest_src_28000.user_id AS user - FROM ***************************.dim_users_latest users_latest_src_28000 - ) subq_22 - ) subq_23 - ON - subq_21.user = subq_23.user - ) subq_24 - ) subq_25 + subq_18.ds__day + , subq_18.ds__week + , subq_18.ds__month + , subq_18.ds__quarter + , subq_18.ds__year + , subq_18.ds__extract_year + , subq_18.ds__extract_quarter + , subq_18.ds__extract_month + , subq_18.ds__extract_day + , subq_18.ds__extract_dow + , subq_18.ds__extract_doy + , subq_18.buy__ds__day + , subq_18.buy__ds__week + , subq_18.buy__ds__month + , subq_18.buy__ds__quarter + , subq_18.buy__ds__year + , subq_18.buy__ds__extract_year + , subq_18.buy__ds__extract_quarter + , subq_18.buy__ds__extract_month + , subq_18.buy__ds__extract_day + , subq_18.buy__ds__extract_dow + , subq_18.buy__ds__extract_doy + , subq_18.metric_time__day + , subq_18.metric_time__week + , subq_18.metric_time__month + , subq_18.metric_time__quarter + , subq_18.metric_time__year + , subq_18.metric_time__extract_year + , subq_18.metric_time__extract_quarter + , subq_18.metric_time__extract_month + , subq_18.metric_time__extract_day + , subq_18.metric_time__extract_dow + , subq_18.metric_time__extract_doy + , subq_18.user + , subq_18.session_id + , subq_18.buy__user + , subq_18.buy__session_id + , subq_18.buys + , subq_18.buyers + , uuid() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_17.ds__day + , subq_17.ds__week + , subq_17.ds__month + , subq_17.ds__quarter + , subq_17.ds__year + , subq_17.ds__extract_year + , subq_17.ds__extract_quarter + , subq_17.ds__extract_month + , subq_17.ds__extract_day + , subq_17.ds__extract_dow + , subq_17.ds__extract_doy + , subq_17.buy__ds__day + , subq_17.buy__ds__week + , subq_17.buy__ds__month + , subq_17.buy__ds__quarter + , subq_17.buy__ds__year + , subq_17.buy__ds__extract_year + , subq_17.buy__ds__extract_quarter + , subq_17.buy__ds__extract_month + , subq_17.buy__ds__extract_day + , subq_17.buy__ds__extract_dow + , subq_17.buy__ds__extract_doy + , subq_17.ds__day AS metric_time__day + , subq_17.ds__week AS metric_time__week + , subq_17.ds__month AS metric_time__month + , subq_17.ds__quarter AS metric_time__quarter + , subq_17.ds__year AS metric_time__year + , subq_17.ds__extract_year AS metric_time__extract_year + , subq_17.ds__extract_quarter AS metric_time__extract_quarter + , subq_17.ds__extract_month AS metric_time__extract_month + , subq_17.ds__extract_day AS metric_time__extract_day + , subq_17.ds__extract_dow AS metric_time__extract_dow + , subq_17.ds__extract_doy AS metric_time__extract_doy + , subq_17.user + , subq_17.session_id + , subq_17.buy__user + , subq_17.buy__session_id + , subq_17.buys + , subq_17.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_28000.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM buys_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_28000.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_28000.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_28000.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_28000.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_28000.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_28000.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_28000.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_28000.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_28000.ds) AS buy__ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM buys_source_src_28000.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_28000.ds) AS buy__ds__extract_doy + , buys_source_src_28000.user_id AS user + , buys_source_src_28000.session_id + , buys_source_src_28000.user_id AS buy__user + , buys_source_src_28000.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_17 + ) subq_18 + ) subq_19 + ON + ( + subq_16.user = subq_19.user + ) AND ( + ( + subq_16.ds__day <= subq_19.ds__day + ) AND ( + subq_16.ds__day > DATE_ADD('day', -7, subq_19.ds__day) + ) + ) + ) subq_20 + ) subq_21 + ) subq_22 GROUP BY - subq_25.metric_time__day - , subq_25.user__home_state_latest - ) subq_26 + subq_22.metric_time__day + , subq_22.user__home_state_latest + ) subq_23 ON ( - subq_9.user__home_state_latest = subq_26.user__home_state_latest + subq_9.user__home_state_latest = subq_23.user__home_state_latest ) AND ( - subq_9.metric_time__day = subq_26.metric_time__day + subq_9.metric_time__day = subq_23.metric_time__day ) GROUP BY - COALESCE(subq_9.metric_time__day, subq_26.metric_time__day) - , COALESCE(subq_9.user__home_state_latest, subq_26.user__home_state_latest) -) subq_27 + COALESCE(subq_9.metric_time__day, subq_23.metric_time__day) + , COALESCE(subq_9.user__home_state_latest, subq_23.user__home_state_latest) +) subq_24 diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_query_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_query_filters__plan0_optimized.sql index 8c837d9664..2510894c6d 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_query_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_conversion_metric_query_filters__plan0_optimized.sql @@ -6,10 +6,10 @@ SELECT FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_37.metric_time__day, subq_54.metric_time__day) AS metric_time__day - , COALESCE(subq_37.user__home_state_latest, subq_54.user__home_state_latest) AS user__home_state_latest - , MAX(subq_37.visits) AS visits - , MAX(subq_54.buys) AS buys + COALESCE(subq_34.metric_time__day, subq_48.metric_time__day) AS metric_time__day + , COALESCE(subq_34.user__home_state_latest, subq_48.user__home_state_latest) AS user__home_state_latest + , MAX(subq_34.visits) AS visits + , MAX(subq_48.buys) AS buys FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'user__home_state_latest', 'metric_time__day'] @@ -22,10 +22,10 @@ FROM ( -- Join Standard Outputs -- Pass Only Elements: ['visits', 'user__home_state_latest', 'visit__referrer_id', 'metric_time__day'] SELECT - subq_30.metric_time__day AS metric_time__day - , subq_30.visit__referrer_id AS visit__referrer_id + subq_27.metric_time__day AS metric_time__day + , subq_27.visit__referrer_id AS visit__referrer_id , users_latest_src_28000.home_state_latest AS user__home_state_latest - , subq_30.visits AS visits + , subq_27.visits AS visits FROM ( -- Read Elements From Semantic Model 'visits_source' -- Metric Time Dimension 'ds' @@ -36,104 +36,115 @@ FROM ( , referrer_id AS visit__referrer_id , 1 AS visits FROM ***************************.fct_visits visits_source_src_28000 - ) subq_30 + ) subq_27 LEFT OUTER JOIN ***************************.dim_users_latest users_latest_src_28000 ON - subq_30.user = users_latest_src_28000.user_id - ) subq_34 + subq_27.user = users_latest_src_28000.user_id + ) subq_31 WHERE visit__referrer_id = '123456' GROUP BY metric_time__day , user__home_state_latest - ) subq_37 + ) subq_34 FULL OUTER JOIN ( - -- Join Standard Outputs + -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'user__home_state_latest', 'metric_time__day'] -- Aggregate Measures SELECT - subq_47.metric_time__day AS metric_time__day - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , SUM(subq_47.buys) AS buys + metric_time__day + , user__home_state_latest + , SUM(buys) AS buys FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_43.visits) OVER ( + FIRST_VALUE(subq_41.visits) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_43.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_41.visit__referrer_id) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_43.user__home_state_latest) OVER ( + , FIRST_VALUE(subq_41.user__home_state_latest) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user__home_state_latest - , FIRST_VALUE(subq_43.ds__day) OVER ( + , FIRST_VALUE(subq_41.ds__day) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day - , FIRST_VALUE(subq_43.metric_time__day) OVER ( + , FIRST_VALUE(subq_41.metric_time__day) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_43.user) OVER ( + , FIRST_VALUE(subq_41.user) OVER ( PARTITION BY - subq_46.user - , subq_46.ds__day - , subq_46.mf_internal_uuid - ORDER BY subq_43.ds__day DESC + subq_44.user + , subq_44.ds__day + , subq_44.mf_internal_uuid + ORDER BY subq_41.ds__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_46.mf_internal_uuid AS mf_internal_uuid - , subq_46.buys AS buys + , subq_44.mf_internal_uuid AS mf_internal_uuid + , subq_44.buys AS buys FROM ( - -- Join Standard Outputs + -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'user__home_state_latest', 'ds__day', 'metric_time__day', 'user'] SELECT - subq_39.ds__day AS ds__day - , subq_39.metric_time__day AS metric_time__day - , subq_39.user AS user - , subq_39.visit__referrer_id AS visit__referrer_id - , users_latest_src_28000.home_state_latest AS user__home_state_latest - , subq_39.visits AS visits + ds__day + , metric_time__day + , subq_39.user + , visit__referrer_id + , user__home_state_latest + , visits FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - DATE_TRUNC('day', ds) AS ds__day - , DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 + subq_36.ds__day AS ds__day + , subq_36.metric_time__day AS metric_time__day + , subq_36.user AS user + , subq_36.visit__referrer_id AS visit__referrer_id + , users_latest_src_28000.home_state_latest AS user__home_state_latest + , subq_36.visits AS visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_36 + LEFT OUTER JOIN + ***************************.dim_users_latest users_latest_src_28000 + ON + subq_36.user = users_latest_src_28000.user_id ) subq_39 - LEFT OUTER JOIN - ***************************.dim_users_latest users_latest_src_28000 - ON - subq_39.user = users_latest_src_28000.user_id - ) subq_43 + WHERE visit__referrer_id = '123456' + ) subq_41 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -144,33 +155,29 @@ FROM ( , 1 AS buys , uuid() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_46 + ) subq_44 ON ( - subq_43.user = subq_46.user + subq_41.user = subq_44.user ) AND ( ( - subq_43.ds__day <= subq_46.ds__day + subq_41.ds__day <= subq_44.ds__day ) AND ( - subq_43.ds__day > DATE_ADD('day', -7, subq_46.ds__day) + subq_41.ds__day > DATE_ADD('day', -7, subq_44.ds__day) ) ) - ) subq_47 - LEFT OUTER JOIN - ***************************.dim_users_latest users_latest_src_28000 - ON - subq_47.user = users_latest_src_28000.user_id + ) subq_45 GROUP BY - subq_47.metric_time__day - , users_latest_src_28000.home_state_latest - ) subq_54 + metric_time__day + , user__home_state_latest + ) subq_48 ON ( - subq_37.user__home_state_latest = subq_54.user__home_state_latest + subq_34.user__home_state_latest = subq_48.user__home_state_latest ) AND ( - subq_37.metric_time__day = subq_54.metric_time__day + subq_34.metric_time__day = subq_48.metric_time__day ) GROUP BY - COALESCE(subq_37.metric_time__day, subq_54.metric_time__day) - , COALESCE(subq_37.user__home_state_latest, subq_54.user__home_state_latest) -) subq_55 + COALESCE(subq_34.metric_time__day, subq_48.metric_time__day) + , COALESCE(subq_34.user__home_state_latest, subq_48.user__home_state_latest) +) subq_49