diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_filter_with_conversion_metric__plan0.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_filter_with_conversion_metric__plan0.sql new file mode 100644 index 0000000000..5d1c993c3a --- /dev/null +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_filter_with_conversion_metric__plan0.sql @@ -0,0 +1,555 @@ +-- Compute Metrics via Expressions +SELECT + subq_39.listings +FROM ( + -- Aggregate Measures + SELECT + SUM(subq_38.listings) AS listings + FROM ( + -- Pass Only Elements: ['listings',] + SELECT + subq_37.listings + FROM ( + -- Constrain Output with WHERE + SELECT + subq_36.user__visit_buy_conversion_rate + , subq_36.listings + FROM ( + -- Pass Only Elements: ['listings', 'user__visit_buy_conversion_rate'] + SELECT + subq_35.user__visit_buy_conversion_rate + , subq_35.listings + FROM ( + -- Join Standard Outputs + SELECT + subq_17.user AS user + , subq_34.user__visit_buy_conversion_rate AS user__visit_buy_conversion_rate + , subq_17.listings AS listings + FROM ( + -- Pass Only Elements: ['listings', 'user'] + SELECT + subq_16.user + , subq_16.listings + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_15.ds__day + , subq_15.ds__week + , subq_15.ds__month + , subq_15.ds__quarter + , subq_15.ds__year + , subq_15.ds__extract_year + , subq_15.ds__extract_quarter + , subq_15.ds__extract_month + , subq_15.ds__extract_day + , subq_15.ds__extract_dow + , subq_15.ds__extract_doy + , subq_15.created_at__day + , subq_15.created_at__week + , subq_15.created_at__month + , subq_15.created_at__quarter + , subq_15.created_at__year + , subq_15.created_at__extract_year + , subq_15.created_at__extract_quarter + , subq_15.created_at__extract_month + , subq_15.created_at__extract_day + , subq_15.created_at__extract_dow + , subq_15.created_at__extract_doy + , subq_15.listing__ds__day + , subq_15.listing__ds__week + , subq_15.listing__ds__month + , subq_15.listing__ds__quarter + , subq_15.listing__ds__year + , subq_15.listing__ds__extract_year + , subq_15.listing__ds__extract_quarter + , subq_15.listing__ds__extract_month + , subq_15.listing__ds__extract_day + , subq_15.listing__ds__extract_dow + , subq_15.listing__ds__extract_doy + , subq_15.listing__created_at__day + , subq_15.listing__created_at__week + , subq_15.listing__created_at__month + , subq_15.listing__created_at__quarter + , subq_15.listing__created_at__year + , subq_15.listing__created_at__extract_year + , subq_15.listing__created_at__extract_quarter + , subq_15.listing__created_at__extract_month + , subq_15.listing__created_at__extract_day + , subq_15.listing__created_at__extract_dow + , subq_15.listing__created_at__extract_doy + , subq_15.ds__day AS metric_time__day + , subq_15.ds__week AS metric_time__week + , subq_15.ds__month AS metric_time__month + , subq_15.ds__quarter AS metric_time__quarter + , subq_15.ds__year AS metric_time__year + , subq_15.ds__extract_year AS metric_time__extract_year + , subq_15.ds__extract_quarter AS metric_time__extract_quarter + , subq_15.ds__extract_month AS metric_time__extract_month + , subq_15.ds__extract_day AS metric_time__extract_day + , subq_15.ds__extract_dow AS metric_time__extract_dow + , subq_15.ds__extract_doy AS metric_time__extract_doy + , subq_15.listing + , subq_15.user + , subq_15.listing__user + , subq_15.country_latest + , subq_15.is_lux_latest + , subq_15.capacity_latest + , subq_15.listing__country_latest + , subq_15.listing__is_lux_latest + , subq_15.listing__capacity_latest + , subq_15.listings + , subq_15.largest_listing + , subq_15.smallest_listing + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + SELECT + 1 AS listings + , listings_latest_src_28000.capacity AS largest_listing + , listings_latest_src_28000.capacity AS smallest_listing + , DATE_TRUNC(listings_latest_src_28000.created_at, day) AS ds__day + , DATE_TRUNC(listings_latest_src_28000.created_at, isoweek) AS ds__week + , DATE_TRUNC(listings_latest_src_28000.created_at, month) AS ds__month + , DATE_TRUNC(listings_latest_src_28000.created_at, quarter) AS ds__quarter + , DATE_TRUNC(listings_latest_src_28000.created_at, year) AS ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) = 1, 7, EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM listings_latest_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC(listings_latest_src_28000.created_at, day) AS created_at__day + , DATE_TRUNC(listings_latest_src_28000.created_at, isoweek) AS created_at__week + , DATE_TRUNC(listings_latest_src_28000.created_at, month) AS created_at__month + , DATE_TRUNC(listings_latest_src_28000.created_at, quarter) AS created_at__quarter + , DATE_TRUNC(listings_latest_src_28000.created_at, year) AS created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS created_at__extract_day + , IF(EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) = 1, 7, EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) - 1) AS created_at__extract_dow + , EXTRACT(dayofyear FROM listings_latest_src_28000.created_at) AS created_at__extract_doy + , listings_latest_src_28000.country AS country_latest + , listings_latest_src_28000.is_lux AS is_lux_latest + , listings_latest_src_28000.capacity AS capacity_latest + , DATE_TRUNC(listings_latest_src_28000.created_at, day) AS listing__ds__day + , DATE_TRUNC(listings_latest_src_28000.created_at, isoweek) AS listing__ds__week + , DATE_TRUNC(listings_latest_src_28000.created_at, month) AS listing__ds__month + , DATE_TRUNC(listings_latest_src_28000.created_at, quarter) AS listing__ds__quarter + , DATE_TRUNC(listings_latest_src_28000.created_at, year) AS listing__ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__ds__extract_day + , IF(EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) = 1, 7, EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) - 1) AS listing__ds__extract_dow + , EXTRACT(dayofyear FROM listings_latest_src_28000.created_at) AS listing__ds__extract_doy + , DATE_TRUNC(listings_latest_src_28000.created_at, day) AS listing__created_at__day + , DATE_TRUNC(listings_latest_src_28000.created_at, isoweek) AS listing__created_at__week + , DATE_TRUNC(listings_latest_src_28000.created_at, month) AS listing__created_at__month + , DATE_TRUNC(listings_latest_src_28000.created_at, quarter) AS listing__created_at__quarter + , DATE_TRUNC(listings_latest_src_28000.created_at, year) AS listing__created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_day + , IF(EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) = 1, 7, EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) - 1) AS listing__created_at__extract_dow + , EXTRACT(dayofyear FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_doy + , listings_latest_src_28000.country AS listing__country_latest + , listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , listings_latest_src_28000.listing_id AS listing + , listings_latest_src_28000.user_id AS user + , listings_latest_src_28000.user_id AS listing__user + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_15 + ) subq_16 + ) subq_17 + LEFT OUTER JOIN ( + -- Pass Only Elements: ['user', 'user__visit_buy_conversion_rate'] + SELECT + subq_33.user + , subq_33.user__visit_buy_conversion_rate + FROM ( + -- Compute Metrics via Expressions + SELECT + subq_32.user + , CAST(subq_32.buys AS FLOAT64) / CAST(NULLIF(subq_32.visits, 0) AS FLOAT64) AS user__visit_buy_conversion_rate + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_21.user, subq_31.user) AS user + , MAX(subq_21.visits) AS visits + , MAX(subq_31.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_20.user + , SUM(subq_20.visits) AS visits + FROM ( + -- Pass Only Elements: ['visits', 'user'] + SELECT + subq_19.user + , subq_19.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + 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.visit__ds__day + , subq_18.visit__ds__week + , subq_18.visit__ds__month + , subq_18.visit__ds__quarter + , subq_18.visit__ds__year + , subq_18.visit__ds__extract_year + , subq_18.visit__ds__extract_quarter + , subq_18.visit__ds__extract_month + , subq_18.visit__ds__extract_day + , subq_18.visit__ds__extract_dow + , subq_18.visit__ds__extract_doy + , subq_18.ds__day AS metric_time__day + , subq_18.ds__week AS metric_time__week + , subq_18.ds__month AS metric_time__month + , subq_18.ds__quarter AS metric_time__quarter + , subq_18.ds__year AS metric_time__year + , subq_18.ds__extract_year AS metric_time__extract_year + , subq_18.ds__extract_quarter AS metric_time__extract_quarter + , subq_18.ds__extract_month AS metric_time__extract_month + , subq_18.ds__extract_day AS metric_time__extract_day + , subq_18.ds__extract_dow AS metric_time__extract_dow + , subq_18.ds__extract_doy AS metric_time__extract_doy + , subq_18.user + , subq_18.session + , subq_18.visit__user + , subq_18.visit__session + , subq_18.referrer_id + , subq_18.visit__referrer_id + , subq_18.visits + , subq_18.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC(visits_source_src_28000.ds, day) AS ds__day + , DATE_TRUNC(visits_source_src_28000.ds, isoweek) AS ds__week + , DATE_TRUNC(visits_source_src_28000.ds, month) AS ds__month + , DATE_TRUNC(visits_source_src_28000.ds, quarter) AS ds__quarter + , DATE_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 + , DATE_TRUNC(visits_source_src_28000.ds, day) AS visit__ds__day + , DATE_TRUNC(visits_source_src_28000.ds, isoweek) AS visit__ds__week + , DATE_TRUNC(visits_source_src_28000.ds, month) AS visit__ds__month + , DATE_TRUNC(visits_source_src_28000.ds, quarter) AS visit__ds__quarter + , DATE_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_18 + ) subq_19 + ) subq_20 + GROUP BY + user + ) subq_21 + FULL OUTER JOIN ( + -- Aggregate Measures + SELECT + subq_30.user + , SUM(subq_30.buys) AS buys + FROM ( + -- Pass Only Elements: ['buys', 'user'] + SELECT + subq_29.user + , subq_29.buys + FROM ( + -- Find conversions for user within the range of INF + SELECT + subq_28.ds__day + , subq_28.user + , subq_28.buys + , subq_28.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_24.visits) OVER ( + PARTITION BY + subq_27.user + , subq_27.ds__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , first_value(subq_24.ds__day) OVER ( + PARTITION BY + subq_27.user + , subq_27.ds__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , first_value(subq_24.user) OVER ( + PARTITION BY + subq_27.user + , subq_27.ds__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_27.mf_internal_uuid AS mf_internal_uuid + , subq_27.buys AS buys + FROM ( + -- Pass Only Elements: ['visits', 'ds__day', 'user'] + SELECT + subq_23.ds__day + , subq_23.user + , subq_23.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_22.ds__day + , subq_22.ds__week + , subq_22.ds__month + , subq_22.ds__quarter + , subq_22.ds__year + , subq_22.ds__extract_year + , subq_22.ds__extract_quarter + , subq_22.ds__extract_month + , subq_22.ds__extract_day + , subq_22.ds__extract_dow + , subq_22.ds__extract_doy + , subq_22.visit__ds__day + , subq_22.visit__ds__week + , subq_22.visit__ds__month + , subq_22.visit__ds__quarter + , subq_22.visit__ds__year + , subq_22.visit__ds__extract_year + , subq_22.visit__ds__extract_quarter + , subq_22.visit__ds__extract_month + , subq_22.visit__ds__extract_day + , subq_22.visit__ds__extract_dow + , subq_22.visit__ds__extract_doy + , subq_22.ds__day AS metric_time__day + , subq_22.ds__week AS metric_time__week + , subq_22.ds__month AS metric_time__month + , subq_22.ds__quarter AS metric_time__quarter + , subq_22.ds__year AS metric_time__year + , subq_22.ds__extract_year AS metric_time__extract_year + , subq_22.ds__extract_quarter AS metric_time__extract_quarter + , subq_22.ds__extract_month AS metric_time__extract_month + , subq_22.ds__extract_day AS metric_time__extract_day + , subq_22.ds__extract_dow AS metric_time__extract_dow + , subq_22.ds__extract_doy AS metric_time__extract_doy + , subq_22.user + , subq_22.session + , subq_22.visit__user + , subq_22.visit__session + , subq_22.referrer_id + , subq_22.visit__referrer_id + , subq_22.visits + , subq_22.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_28000.user_id AS visitors + , DATE_TRUNC(visits_source_src_28000.ds, day) AS ds__day + , DATE_TRUNC(visits_source_src_28000.ds, isoweek) AS ds__week + , DATE_TRUNC(visits_source_src_28000.ds, month) AS ds__month + , DATE_TRUNC(visits_source_src_28000.ds, quarter) AS ds__quarter + , DATE_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 + , DATE_TRUNC(visits_source_src_28000.ds, day) AS visit__ds__day + , DATE_TRUNC(visits_source_src_28000.ds, isoweek) AS visit__ds__week + , DATE_TRUNC(visits_source_src_28000.ds, month) AS visit__ds__month + , DATE_TRUNC(visits_source_src_28000.ds, quarter) AS visit__ds__quarter + , DATE_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_22 + ) subq_23 + ) subq_24 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_26.ds__day + , subq_26.ds__week + , subq_26.ds__month + , subq_26.ds__quarter + , subq_26.ds__year + , subq_26.ds__extract_year + , subq_26.ds__extract_quarter + , subq_26.ds__extract_month + , subq_26.ds__extract_day + , subq_26.ds__extract_dow + , subq_26.ds__extract_doy + , subq_26.buy__ds__day + , subq_26.buy__ds__week + , subq_26.buy__ds__month + , subq_26.buy__ds__quarter + , subq_26.buy__ds__year + , subq_26.buy__ds__extract_year + , subq_26.buy__ds__extract_quarter + , subq_26.buy__ds__extract_month + , subq_26.buy__ds__extract_day + , subq_26.buy__ds__extract_dow + , subq_26.buy__ds__extract_doy + , subq_26.metric_time__day + , subq_26.metric_time__week + , subq_26.metric_time__month + , subq_26.metric_time__quarter + , subq_26.metric_time__year + , subq_26.metric_time__extract_year + , subq_26.metric_time__extract_quarter + , subq_26.metric_time__extract_month + , subq_26.metric_time__extract_day + , subq_26.metric_time__extract_dow + , subq_26.metric_time__extract_doy + , subq_26.user + , subq_26.session_id + , subq_26.buy__user + , subq_26.buy__session_id + , subq_26.buys + , subq_26.buyers + , GENERATE_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_25.ds__day + , subq_25.ds__week + , subq_25.ds__month + , subq_25.ds__quarter + , subq_25.ds__year + , subq_25.ds__extract_year + , subq_25.ds__extract_quarter + , subq_25.ds__extract_month + , subq_25.ds__extract_day + , subq_25.ds__extract_dow + , subq_25.ds__extract_doy + , subq_25.buy__ds__day + , subq_25.buy__ds__week + , subq_25.buy__ds__month + , subq_25.buy__ds__quarter + , subq_25.buy__ds__year + , subq_25.buy__ds__extract_year + , subq_25.buy__ds__extract_quarter + , subq_25.buy__ds__extract_month + , subq_25.buy__ds__extract_day + , subq_25.buy__ds__extract_dow + , subq_25.buy__ds__extract_doy + , subq_25.ds__day AS metric_time__day + , subq_25.ds__week AS metric_time__week + , subq_25.ds__month AS metric_time__month + , subq_25.ds__quarter AS metric_time__quarter + , subq_25.ds__year AS metric_time__year + , subq_25.ds__extract_year AS metric_time__extract_year + , subq_25.ds__extract_quarter AS metric_time__extract_quarter + , subq_25.ds__extract_month AS metric_time__extract_month + , subq_25.ds__extract_day AS metric_time__extract_day + , subq_25.ds__extract_dow AS metric_time__extract_dow + , subq_25.ds__extract_doy AS metric_time__extract_doy + , subq_25.user + , subq_25.session_id + , subq_25.buy__user + , subq_25.buy__session_id + , subq_25.buys + , subq_25.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_28000.user_id AS buyers + , DATE_TRUNC(buys_source_src_28000.ds, day) AS ds__day + , DATE_TRUNC(buys_source_src_28000.ds, isoweek) AS ds__week + , DATE_TRUNC(buys_source_src_28000.ds, month) AS ds__month + , DATE_TRUNC(buys_source_src_28000.ds, quarter) AS ds__quarter + , DATE_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 + , DATE_TRUNC(buys_source_src_28000.ds, day) AS buy__ds__day + , DATE_TRUNC(buys_source_src_28000.ds, isoweek) AS buy__ds__week + , DATE_TRUNC(buys_source_src_28000.ds, month) AS buy__ds__month + , DATE_TRUNC(buys_source_src_28000.ds, quarter) AS buy__ds__quarter + , DATE_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_25 + ) subq_26 + ) subq_27 + ON + ( + subq_24.user = subq_27.user + ) AND ( + (subq_24.ds__day <= subq_27.ds__day) + ) + ) subq_28 + ) subq_29 + ) subq_30 + GROUP BY + user + ) subq_31 + ON + subq_21.user = subq_31.user + GROUP BY + user + ) subq_32 + ) subq_33 + ) subq_34 + ON + subq_17.user = subq_34.user + ) subq_35 + ) subq_36 + WHERE user__visit_buy_conversion_rate > 2 + ) subq_37 + ) subq_38 +) subq_39 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_filter_with_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_filter_with_conversion_metric__plan0_optimized.sql new file mode 100644 index 0000000000..42f8341179 --- /dev/null +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_filter_with_conversion_metric__plan0_optimized.sql @@ -0,0 +1,120 @@ +-- Constrain Output with WHERE +-- Pass Only Elements: ['listings',] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + SUM(listings) AS listings +FROM ( + -- Join Standard Outputs + -- Pass Only Elements: ['listings', 'user__visit_buy_conversion_rate'] + SELECT + CAST(subq_57.buys AS FLOAT64) / CAST(NULLIF(subq_57.visits, 0) AS FLOAT64) AS user__visit_buy_conversion_rate + , subq_42.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['listings', 'user'] + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_42 + LEFT OUTER JOIN ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_46.user, subq_56.user) AS user + , MAX(subq_46.visits) AS visits + , MAX(subq_56.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_45.user + , SUM(visits) AS visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['visits', 'user'] + SELECT + user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_45 + GROUP BY + user + ) subq_46 + FULL OUTER JOIN ( + -- Find conversions for user within the range of INF + -- Pass Only Elements: ['buys', 'user'] + -- Aggregate Measures + SELECT + subq_53.user + , SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_49.visits) OVER ( + PARTITION BY + subq_52.user + , subq_52.ds__day + , subq_52.mf_internal_uuid + ORDER BY subq_49.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , first_value(subq_49.ds__day) OVER ( + PARTITION BY + subq_52.user + , subq_52.ds__day + , subq_52.mf_internal_uuid + ORDER BY subq_49.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , first_value(subq_49.user) OVER ( + PARTITION BY + subq_52.user + , subq_52.ds__day + , subq_52.mf_internal_uuid + ORDER BY subq_49.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_52.mf_internal_uuid AS mf_internal_uuid + , subq_52.buys AS buys + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['visits', 'ds__day', 'user'] + SELECT + DATE_TRUNC(ds, day) AS ds__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_49 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC(ds, day) AS ds__day + , user_id AS user + , 1 AS buys + , GENERATE_UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_28000 + ) subq_52 + ON + ( + subq_49.user = subq_52.user + ) AND ( + (subq_49.ds__day <= subq_52.ds__day) + ) + ) subq_53 + GROUP BY + user + ) subq_56 + ON + subq_46.user = subq_56.user + GROUP BY + user + ) subq_57 + ON + subq_42.user = subq_57.user +) subq_61 +WHERE user__visit_buy_conversion_rate > 2 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_group_by_has_local_entity_prefix2__plan0.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_group_by_has_local_entity_prefix2__plan0.sql new file mode 100644 index 0000000000..b065bf8cea --- /dev/null +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_group_by_has_local_entity_prefix2__plan0.sql @@ -0,0 +1,318 @@ +-- Compute Metrics via Expressions +SELECT + subq_17.listings +FROM ( + -- Aggregate Measures + SELECT + SUM(subq_16.listings) AS listings + FROM ( + -- Pass Only Elements: ['listings',] + SELECT + subq_15.listings + FROM ( + -- Constrain Output with WHERE + SELECT + subq_14.listing__view__listing__views + , subq_14.listings + FROM ( + -- Pass Only Elements: ['listings', 'listing__view__listing__views'] + SELECT + subq_13.listing__view__listing__views + , subq_13.listings + FROM ( + -- Join Standard Outputs + SELECT + subq_6.listing AS listing + , subq_12.view__listing AS listing__view__listing + , subq_12.view__listing__views AS listing__view__listing__views + , subq_6.listings AS listings + FROM ( + -- Pass Only Elements: ['listings', 'listing'] + SELECT + subq_5.listing + , subq_5.listings + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.created_at__day + , subq_4.created_at__week + , subq_4.created_at__month + , subq_4.created_at__quarter + , subq_4.created_at__year + , subq_4.created_at__extract_year + , subq_4.created_at__extract_quarter + , subq_4.created_at__extract_month + , subq_4.created_at__extract_day + , subq_4.created_at__extract_dow + , subq_4.created_at__extract_doy + , subq_4.listing__ds__day + , subq_4.listing__ds__week + , subq_4.listing__ds__month + , subq_4.listing__ds__quarter + , subq_4.listing__ds__year + , subq_4.listing__ds__extract_year + , subq_4.listing__ds__extract_quarter + , subq_4.listing__ds__extract_month + , subq_4.listing__ds__extract_day + , subq_4.listing__ds__extract_dow + , subq_4.listing__ds__extract_doy + , subq_4.listing__created_at__day + , subq_4.listing__created_at__week + , subq_4.listing__created_at__month + , subq_4.listing__created_at__quarter + , subq_4.listing__created_at__year + , subq_4.listing__created_at__extract_year + , subq_4.listing__created_at__extract_quarter + , subq_4.listing__created_at__extract_month + , subq_4.listing__created_at__extract_day + , subq_4.listing__created_at__extract_dow + , subq_4.listing__created_at__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.listing + , subq_4.user + , subq_4.listing__user + , subq_4.country_latest + , subq_4.is_lux_latest + , subq_4.capacity_latest + , subq_4.listing__country_latest + , subq_4.listing__is_lux_latest + , subq_4.listing__capacity_latest + , subq_4.listings + , subq_4.largest_listing + , subq_4.smallest_listing + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + SELECT + 1 AS listings + , listings_latest_src_28000.capacity AS largest_listing + , listings_latest_src_28000.capacity AS smallest_listing + , DATE_TRUNC(listings_latest_src_28000.created_at, day) AS ds__day + , DATE_TRUNC(listings_latest_src_28000.created_at, isoweek) AS ds__week + , DATE_TRUNC(listings_latest_src_28000.created_at, month) AS ds__month + , DATE_TRUNC(listings_latest_src_28000.created_at, quarter) AS ds__quarter + , DATE_TRUNC(listings_latest_src_28000.created_at, year) AS ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) = 1, 7, EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM listings_latest_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC(listings_latest_src_28000.created_at, day) AS created_at__day + , DATE_TRUNC(listings_latest_src_28000.created_at, isoweek) AS created_at__week + , DATE_TRUNC(listings_latest_src_28000.created_at, month) AS created_at__month + , DATE_TRUNC(listings_latest_src_28000.created_at, quarter) AS created_at__quarter + , DATE_TRUNC(listings_latest_src_28000.created_at, year) AS created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS created_at__extract_day + , IF(EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) = 1, 7, EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) - 1) AS created_at__extract_dow + , EXTRACT(dayofyear FROM listings_latest_src_28000.created_at) AS created_at__extract_doy + , listings_latest_src_28000.country AS country_latest + , listings_latest_src_28000.is_lux AS is_lux_latest + , listings_latest_src_28000.capacity AS capacity_latest + , DATE_TRUNC(listings_latest_src_28000.created_at, day) AS listing__ds__day + , DATE_TRUNC(listings_latest_src_28000.created_at, isoweek) AS listing__ds__week + , DATE_TRUNC(listings_latest_src_28000.created_at, month) AS listing__ds__month + , DATE_TRUNC(listings_latest_src_28000.created_at, quarter) AS listing__ds__quarter + , DATE_TRUNC(listings_latest_src_28000.created_at, year) AS listing__ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__ds__extract_day + , IF(EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) = 1, 7, EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) - 1) AS listing__ds__extract_dow + , EXTRACT(dayofyear FROM listings_latest_src_28000.created_at) AS listing__ds__extract_doy + , DATE_TRUNC(listings_latest_src_28000.created_at, day) AS listing__created_at__day + , DATE_TRUNC(listings_latest_src_28000.created_at, isoweek) AS listing__created_at__week + , DATE_TRUNC(listings_latest_src_28000.created_at, month) AS listing__created_at__month + , DATE_TRUNC(listings_latest_src_28000.created_at, quarter) AS listing__created_at__quarter + , DATE_TRUNC(listings_latest_src_28000.created_at, year) AS listing__created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_day + , IF(EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) = 1, 7, EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) - 1) AS listing__created_at__extract_dow + , EXTRACT(dayofyear FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_doy + , listings_latest_src_28000.country AS listing__country_latest + , listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , listings_latest_src_28000.listing_id AS listing + , listings_latest_src_28000.user_id AS user + , listings_latest_src_28000.user_id AS listing__user + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_4 + ) subq_5 + ) subq_6 + LEFT OUTER JOIN ( + -- Pass Only Elements: ['view__listing', 'view__listing__views'] + SELECT + subq_11.view__listing + , subq_11.view__listing__views + FROM ( + -- Compute Metrics via Expressions + SELECT + subq_10.view__listing + , subq_10.views AS view__listing__views + FROM ( + -- Aggregate Measures + SELECT + subq_9.view__listing + , SUM(subq_9.views) AS views + FROM ( + -- Pass Only Elements: ['views', 'view__listing'] + SELECT + subq_8.view__listing + , subq_8.views + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.ds_partitioned__day + , subq_7.ds_partitioned__week + , subq_7.ds_partitioned__month + , subq_7.ds_partitioned__quarter + , subq_7.ds_partitioned__year + , subq_7.ds_partitioned__extract_year + , subq_7.ds_partitioned__extract_quarter + , subq_7.ds_partitioned__extract_month + , subq_7.ds_partitioned__extract_day + , subq_7.ds_partitioned__extract_dow + , subq_7.ds_partitioned__extract_doy + , subq_7.view__ds__day + , subq_7.view__ds__week + , subq_7.view__ds__month + , subq_7.view__ds__quarter + , subq_7.view__ds__year + , subq_7.view__ds__extract_year + , subq_7.view__ds__extract_quarter + , subq_7.view__ds__extract_month + , subq_7.view__ds__extract_day + , subq_7.view__ds__extract_dow + , subq_7.view__ds__extract_doy + , subq_7.view__ds_partitioned__day + , subq_7.view__ds_partitioned__week + , subq_7.view__ds_partitioned__month + , subq_7.view__ds_partitioned__quarter + , subq_7.view__ds_partitioned__year + , subq_7.view__ds_partitioned__extract_year + , subq_7.view__ds_partitioned__extract_quarter + , subq_7.view__ds_partitioned__extract_month + , subq_7.view__ds_partitioned__extract_day + , subq_7.view__ds_partitioned__extract_dow + , subq_7.view__ds_partitioned__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.listing + , subq_7.user + , subq_7.view__listing + , subq_7.view__user + , subq_7.views + FROM ( + -- Read Elements From Semantic Model 'views_source' + SELECT + 1 AS views + , DATE_TRUNC(views_source_src_28000.ds, day) AS ds__day + , DATE_TRUNC(views_source_src_28000.ds, isoweek) AS ds__week + , DATE_TRUNC(views_source_src_28000.ds, month) AS ds__month + , DATE_TRUNC(views_source_src_28000.ds, quarter) AS ds__quarter + , DATE_TRUNC(views_source_src_28000.ds, year) AS ds__year + , EXTRACT(year FROM views_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM views_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM views_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM views_source_src_28000.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM views_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM views_source_src_28000.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM views_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC(views_source_src_28000.ds_partitioned, day) AS ds_partitioned__day + , DATE_TRUNC(views_source_src_28000.ds_partitioned, isoweek) AS ds_partitioned__week + , DATE_TRUNC(views_source_src_28000.ds_partitioned, month) AS ds_partitioned__month + , DATE_TRUNC(views_source_src_28000.ds_partitioned, quarter) AS ds_partitioned__quarter + , DATE_TRUNC(views_source_src_28000.ds_partitioned, year) AS ds_partitioned__year + , EXTRACT(year FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , IF(EXTRACT(dayofweek FROM views_source_src_28000.ds_partitioned) = 1, 7, EXTRACT(dayofweek FROM views_source_src_28000.ds_partitioned) - 1) AS ds_partitioned__extract_dow + , EXTRACT(dayofyear FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC(views_source_src_28000.ds, day) AS view__ds__day + , DATE_TRUNC(views_source_src_28000.ds, isoweek) AS view__ds__week + , DATE_TRUNC(views_source_src_28000.ds, month) AS view__ds__month + , DATE_TRUNC(views_source_src_28000.ds, quarter) AS view__ds__quarter + , DATE_TRUNC(views_source_src_28000.ds, year) AS view__ds__year + , EXTRACT(year FROM views_source_src_28000.ds) AS view__ds__extract_year + , EXTRACT(quarter FROM views_source_src_28000.ds) AS view__ds__extract_quarter + , EXTRACT(month FROM views_source_src_28000.ds) AS view__ds__extract_month + , EXTRACT(day FROM views_source_src_28000.ds) AS view__ds__extract_day + , IF(EXTRACT(dayofweek FROM views_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM views_source_src_28000.ds) - 1) AS view__ds__extract_dow + , EXTRACT(dayofyear FROM views_source_src_28000.ds) AS view__ds__extract_doy + , DATE_TRUNC(views_source_src_28000.ds_partitioned, day) AS view__ds_partitioned__day + , DATE_TRUNC(views_source_src_28000.ds_partitioned, isoweek) AS view__ds_partitioned__week + , DATE_TRUNC(views_source_src_28000.ds_partitioned, month) AS view__ds_partitioned__month + , DATE_TRUNC(views_source_src_28000.ds_partitioned, quarter) AS view__ds_partitioned__quarter + , DATE_TRUNC(views_source_src_28000.ds_partitioned, year) AS view__ds_partitioned__year + , EXTRACT(year FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_year + , EXTRACT(quarter FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_quarter + , EXTRACT(month FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_month + , EXTRACT(day FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_day + , IF(EXTRACT(dayofweek FROM views_source_src_28000.ds_partitioned) = 1, 7, EXTRACT(dayofweek FROM views_source_src_28000.ds_partitioned) - 1) AS view__ds_partitioned__extract_dow + , EXTRACT(dayofyear FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_doy + , views_source_src_28000.listing_id AS listing + , views_source_src_28000.user_id AS user + , views_source_src_28000.listing_id AS view__listing + , views_source_src_28000.user_id AS view__user + FROM ***************************.fct_views views_source_src_28000 + ) subq_7 + ) subq_8 + ) subq_9 + GROUP BY + view__listing + ) subq_10 + ) subq_11 + ) subq_12 + ON + subq_6.listing = subq_12.view__listing + ) subq_13 + ) subq_14 + WHERE listing__view__listing__views > 2 + ) subq_15 + ) subq_16 +) subq_17 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_group_by_has_local_entity_prefix2__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_group_by_has_local_entity_prefix2__plan0_optimized.sql new file mode 100644 index 0000000000..05fd368fbd --- /dev/null +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_group_by_has_local_entity_prefix2__plan0_optimized.sql @@ -0,0 +1,44 @@ +-- Constrain Output with WHERE +-- Pass Only Elements: ['listings',] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + SUM(listings) AS listings +FROM ( + -- Join Standard Outputs + -- Pass Only Elements: ['listings', 'listing__view__listing__views'] + SELECT + subq_26.view__listing__views AS listing__view__listing__views + , subq_20.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['listings', 'listing'] + SELECT + listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN ( + -- Aggregate Measures + -- Compute Metrics via Expressions + -- Pass Only Elements: ['view__listing', 'view__listing__views'] + SELECT + view__listing + , SUM(views) AS view__listing__views + FROM ( + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['views', 'view__listing'] + SELECT + listing_id AS view__listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_23 + GROUP BY + view__listing + ) subq_26 + ON + subq_20.listing = subq_26.view__listing +) subq_28 +WHERE listing__view__listing__views > 2 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_filter_with_conversion_metric__plan0.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_filter_with_conversion_metric__plan0.sql new file mode 100644 index 0000000000..e76b8b835c --- /dev/null +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_filter_with_conversion_metric__plan0.sql @@ -0,0 +1,555 @@ +-- Compute Metrics via Expressions +SELECT + subq_39.listings +FROM ( + -- Aggregate Measures + SELECT + SUM(subq_38.listings) AS listings + FROM ( + -- Pass Only Elements: ['listings',] + SELECT + subq_37.listings + FROM ( + -- Constrain Output with WHERE + SELECT + subq_36.user__visit_buy_conversion_rate + , subq_36.listings + FROM ( + -- Pass Only Elements: ['listings', 'user__visit_buy_conversion_rate'] + SELECT + subq_35.user__visit_buy_conversion_rate + , subq_35.listings + FROM ( + -- Join Standard Outputs + SELECT + subq_17.user AS user + , subq_34.user__visit_buy_conversion_rate AS user__visit_buy_conversion_rate + , subq_17.listings AS listings + FROM ( + -- Pass Only Elements: ['listings', 'user'] + SELECT + subq_16.user + , subq_16.listings + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_15.ds__day + , subq_15.ds__week + , subq_15.ds__month + , subq_15.ds__quarter + , subq_15.ds__year + , subq_15.ds__extract_year + , subq_15.ds__extract_quarter + , subq_15.ds__extract_month + , subq_15.ds__extract_day + , subq_15.ds__extract_dow + , subq_15.ds__extract_doy + , subq_15.created_at__day + , subq_15.created_at__week + , subq_15.created_at__month + , subq_15.created_at__quarter + , subq_15.created_at__year + , subq_15.created_at__extract_year + , subq_15.created_at__extract_quarter + , subq_15.created_at__extract_month + , subq_15.created_at__extract_day + , subq_15.created_at__extract_dow + , subq_15.created_at__extract_doy + , subq_15.listing__ds__day + , subq_15.listing__ds__week + , subq_15.listing__ds__month + , subq_15.listing__ds__quarter + , subq_15.listing__ds__year + , subq_15.listing__ds__extract_year + , subq_15.listing__ds__extract_quarter + , subq_15.listing__ds__extract_month + , subq_15.listing__ds__extract_day + , subq_15.listing__ds__extract_dow + , subq_15.listing__ds__extract_doy + , subq_15.listing__created_at__day + , subq_15.listing__created_at__week + , subq_15.listing__created_at__month + , subq_15.listing__created_at__quarter + , subq_15.listing__created_at__year + , subq_15.listing__created_at__extract_year + , subq_15.listing__created_at__extract_quarter + , subq_15.listing__created_at__extract_month + , subq_15.listing__created_at__extract_day + , subq_15.listing__created_at__extract_dow + , subq_15.listing__created_at__extract_doy + , subq_15.ds__day AS metric_time__day + , subq_15.ds__week AS metric_time__week + , subq_15.ds__month AS metric_time__month + , subq_15.ds__quarter AS metric_time__quarter + , subq_15.ds__year AS metric_time__year + , subq_15.ds__extract_year AS metric_time__extract_year + , subq_15.ds__extract_quarter AS metric_time__extract_quarter + , subq_15.ds__extract_month AS metric_time__extract_month + , subq_15.ds__extract_day AS metric_time__extract_day + , subq_15.ds__extract_dow AS metric_time__extract_dow + , subq_15.ds__extract_doy AS metric_time__extract_doy + , subq_15.listing + , subq_15.user + , subq_15.listing__user + , subq_15.country_latest + , subq_15.is_lux_latest + , subq_15.capacity_latest + , subq_15.listing__country_latest + , subq_15.listing__is_lux_latest + , subq_15.listing__capacity_latest + , subq_15.listings + , subq_15.largest_listing + , subq_15.smallest_listing + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + SELECT + 1 AS listings + , listings_latest_src_28000.capacity AS largest_listing + , listings_latest_src_28000.capacity AS smallest_listing + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM listings_latest_src_28000.created_at) AS ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS created_at__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM listings_latest_src_28000.created_at) AS created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS created_at__extract_doy + , listings_latest_src_28000.country AS country_latest + , listings_latest_src_28000.is_lux AS is_lux_latest + , listings_latest_src_28000.capacity AS capacity_latest + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM listings_latest_src_28000.created_at) AS listing__ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_doy + , listings_latest_src_28000.country AS listing__country_latest + , listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , listings_latest_src_28000.listing_id AS listing + , listings_latest_src_28000.user_id AS user + , listings_latest_src_28000.user_id AS listing__user + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_15 + ) subq_16 + ) subq_17 + LEFT OUTER JOIN ( + -- Pass Only Elements: ['user', 'user__visit_buy_conversion_rate'] + SELECT + subq_33.user + , subq_33.user__visit_buy_conversion_rate + FROM ( + -- Compute Metrics via Expressions + SELECT + subq_32.user + , CAST(subq_32.buys AS DOUBLE) / CAST(NULLIF(subq_32.visits, 0) AS DOUBLE) AS user__visit_buy_conversion_rate + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_21.user, subq_31.user) AS user + , MAX(subq_21.visits) AS visits + , MAX(subq_31.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_20.user + , SUM(subq_20.visits) AS visits + FROM ( + -- Pass Only Elements: ['visits', 'user'] + SELECT + subq_19.user + , subq_19.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + 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.visit__ds__day + , subq_18.visit__ds__week + , subq_18.visit__ds__month + , subq_18.visit__ds__quarter + , subq_18.visit__ds__year + , subq_18.visit__ds__extract_year + , subq_18.visit__ds__extract_quarter + , subq_18.visit__ds__extract_month + , subq_18.visit__ds__extract_day + , subq_18.visit__ds__extract_dow + , subq_18.visit__ds__extract_doy + , subq_18.ds__day AS metric_time__day + , subq_18.ds__week AS metric_time__week + , subq_18.ds__month AS metric_time__month + , subq_18.ds__quarter AS metric_time__quarter + , subq_18.ds__year AS metric_time__year + , subq_18.ds__extract_year AS metric_time__extract_year + , subq_18.ds__extract_quarter AS metric_time__extract_quarter + , subq_18.ds__extract_month AS metric_time__extract_month + , subq_18.ds__extract_day AS metric_time__extract_day + , subq_18.ds__extract_dow AS metric_time__extract_dow + , subq_18.ds__extract_doy AS metric_time__extract_doy + , subq_18.user + , subq_18.session + , subq_18.visit__user + , subq_18.visit__session + , subq_18.referrer_id + , subq_18.visit__referrer_id + , subq_18.visits + , subq_18.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_18 + ) subq_19 + ) subq_20 + GROUP BY + subq_20.user + ) subq_21 + FULL OUTER JOIN ( + -- Aggregate Measures + SELECT + subq_30.user + , SUM(subq_30.buys) AS buys + FROM ( + -- Pass Only Elements: ['buys', 'user'] + SELECT + subq_29.user + , subq_29.buys + FROM ( + -- Find conversions for user within the range of INF + SELECT + subq_28.ds__day + , subq_28.user + , subq_28.buys + , subq_28.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_24.visits) OVER ( + PARTITION BY + subq_27.user + , subq_27.ds__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , first_value(subq_24.ds__day) OVER ( + PARTITION BY + subq_27.user + , subq_27.ds__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , first_value(subq_24.user) OVER ( + PARTITION BY + subq_27.user + , subq_27.ds__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_27.mf_internal_uuid AS mf_internal_uuid + , subq_27.buys AS buys + FROM ( + -- Pass Only Elements: ['visits', 'ds__day', 'user'] + SELECT + subq_23.ds__day + , subq_23.user + , subq_23.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_22.ds__day + , subq_22.ds__week + , subq_22.ds__month + , subq_22.ds__quarter + , subq_22.ds__year + , subq_22.ds__extract_year + , subq_22.ds__extract_quarter + , subq_22.ds__extract_month + , subq_22.ds__extract_day + , subq_22.ds__extract_dow + , subq_22.ds__extract_doy + , subq_22.visit__ds__day + , subq_22.visit__ds__week + , subq_22.visit__ds__month + , subq_22.visit__ds__quarter + , subq_22.visit__ds__year + , subq_22.visit__ds__extract_year + , subq_22.visit__ds__extract_quarter + , subq_22.visit__ds__extract_month + , subq_22.visit__ds__extract_day + , subq_22.visit__ds__extract_dow + , subq_22.visit__ds__extract_doy + , subq_22.ds__day AS metric_time__day + , subq_22.ds__week AS metric_time__week + , subq_22.ds__month AS metric_time__month + , subq_22.ds__quarter AS metric_time__quarter + , subq_22.ds__year AS metric_time__year + , subq_22.ds__extract_year AS metric_time__extract_year + , subq_22.ds__extract_quarter AS metric_time__extract_quarter + , subq_22.ds__extract_month AS metric_time__extract_month + , subq_22.ds__extract_day AS metric_time__extract_day + , subq_22.ds__extract_dow AS metric_time__extract_dow + , subq_22.ds__extract_doy AS metric_time__extract_doy + , subq_22.user + , subq_22.session + , subq_22.visit__user + , subq_22.visit__session + , subq_22.referrer_id + , subq_22.visit__referrer_id + , subq_22.visits + , subq_22.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_22 + ) subq_23 + ) subq_24 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_26.ds__day + , subq_26.ds__week + , subq_26.ds__month + , subq_26.ds__quarter + , subq_26.ds__year + , subq_26.ds__extract_year + , subq_26.ds__extract_quarter + , subq_26.ds__extract_month + , subq_26.ds__extract_day + , subq_26.ds__extract_dow + , subq_26.ds__extract_doy + , subq_26.buy__ds__day + , subq_26.buy__ds__week + , subq_26.buy__ds__month + , subq_26.buy__ds__quarter + , subq_26.buy__ds__year + , subq_26.buy__ds__extract_year + , subq_26.buy__ds__extract_quarter + , subq_26.buy__ds__extract_month + , subq_26.buy__ds__extract_day + , subq_26.buy__ds__extract_dow + , subq_26.buy__ds__extract_doy + , subq_26.metric_time__day + , subq_26.metric_time__week + , subq_26.metric_time__month + , subq_26.metric_time__quarter + , subq_26.metric_time__year + , subq_26.metric_time__extract_year + , subq_26.metric_time__extract_quarter + , subq_26.metric_time__extract_month + , subq_26.metric_time__extract_day + , subq_26.metric_time__extract_dow + , subq_26.metric_time__extract_doy + , subq_26.user + , subq_26.session_id + , subq_26.buy__user + , subq_26.buy__session_id + , subq_26.buys + , subq_26.buyers + , UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_25.ds__day + , subq_25.ds__week + , subq_25.ds__month + , subq_25.ds__quarter + , subq_25.ds__year + , subq_25.ds__extract_year + , subq_25.ds__extract_quarter + , subq_25.ds__extract_month + , subq_25.ds__extract_day + , subq_25.ds__extract_dow + , subq_25.ds__extract_doy + , subq_25.buy__ds__day + , subq_25.buy__ds__week + , subq_25.buy__ds__month + , subq_25.buy__ds__quarter + , subq_25.buy__ds__year + , subq_25.buy__ds__extract_year + , subq_25.buy__ds__extract_quarter + , subq_25.buy__ds__extract_month + , subq_25.buy__ds__extract_day + , subq_25.buy__ds__extract_dow + , subq_25.buy__ds__extract_doy + , subq_25.ds__day AS metric_time__day + , subq_25.ds__week AS metric_time__week + , subq_25.ds__month AS metric_time__month + , subq_25.ds__quarter AS metric_time__quarter + , subq_25.ds__year AS metric_time__year + , subq_25.ds__extract_year AS metric_time__extract_year + , subq_25.ds__extract_quarter AS metric_time__extract_quarter + , subq_25.ds__extract_month AS metric_time__extract_month + , subq_25.ds__extract_day AS metric_time__extract_day + , subq_25.ds__extract_dow AS metric_time__extract_dow + , subq_25.ds__extract_doy AS metric_time__extract_doy + , subq_25.user + , subq_25.session_id + , subq_25.buy__user + , subq_25.buy__session_id + , subq_25.buys + , subq_25.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_25 + ) subq_26 + ) subq_27 + ON + ( + subq_24.user = subq_27.user + ) AND ( + (subq_24.ds__day <= subq_27.ds__day) + ) + ) subq_28 + ) subq_29 + ) subq_30 + GROUP BY + subq_30.user + ) subq_31 + ON + subq_21.user = subq_31.user + GROUP BY + COALESCE(subq_21.user, subq_31.user) + ) subq_32 + ) subq_33 + ) subq_34 + ON + subq_17.user = subq_34.user + ) subq_35 + ) subq_36 + WHERE user__visit_buy_conversion_rate > 2 + ) subq_37 + ) subq_38 +) subq_39 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_filter_with_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_filter_with_conversion_metric__plan0_optimized.sql new file mode 100644 index 0000000000..ca489133ee --- /dev/null +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_filter_with_conversion_metric__plan0_optimized.sql @@ -0,0 +1,120 @@ +-- Constrain Output with WHERE +-- Pass Only Elements: ['listings',] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + SUM(listings) AS listings +FROM ( + -- Join Standard Outputs + -- Pass Only Elements: ['listings', 'user__visit_buy_conversion_rate'] + SELECT + CAST(subq_57.buys AS DOUBLE) / CAST(NULLIF(subq_57.visits, 0) AS DOUBLE) AS user__visit_buy_conversion_rate + , subq_42.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['listings', 'user'] + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_42 + LEFT OUTER JOIN ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_46.user, subq_56.user) AS user + , MAX(subq_46.visits) AS visits + , MAX(subq_56.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_45.user + , SUM(visits) AS visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['visits', 'user'] + SELECT + user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_45 + GROUP BY + subq_45.user + ) subq_46 + FULL OUTER JOIN ( + -- Find conversions for user within the range of INF + -- Pass Only Elements: ['buys', 'user'] + -- Aggregate Measures + SELECT + subq_53.user + , SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_49.visits) OVER ( + PARTITION BY + subq_52.user + , subq_52.ds__day + , subq_52.mf_internal_uuid + ORDER BY subq_49.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , first_value(subq_49.ds__day) OVER ( + PARTITION BY + subq_52.user + , subq_52.ds__day + , subq_52.mf_internal_uuid + ORDER BY subq_49.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , first_value(subq_49.user) OVER ( + PARTITION BY + subq_52.user + , subq_52.ds__day + , subq_52.mf_internal_uuid + ORDER BY subq_49.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_52.mf_internal_uuid AS mf_internal_uuid + , subq_52.buys AS buys + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['visits', 'ds__day', 'user'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_49 + 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_52 + ON + ( + subq_49.user = subq_52.user + ) AND ( + (subq_49.ds__day <= subq_52.ds__day) + ) + ) subq_53 + GROUP BY + subq_53.user + ) subq_56 + ON + subq_46.user = subq_56.user + GROUP BY + COALESCE(subq_46.user, subq_56.user) + ) subq_57 + ON + subq_42.user = subq_57.user +) subq_61 +WHERE user__visit_buy_conversion_rate > 2 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_group_by_has_local_entity_prefix2__plan0.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_group_by_has_local_entity_prefix2__plan0.sql new file mode 100644 index 0000000000..48166518d4 --- /dev/null +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_group_by_has_local_entity_prefix2__plan0.sql @@ -0,0 +1,318 @@ +-- Compute Metrics via Expressions +SELECT + subq_17.listings +FROM ( + -- Aggregate Measures + SELECT + SUM(subq_16.listings) AS listings + FROM ( + -- Pass Only Elements: ['listings',] + SELECT + subq_15.listings + FROM ( + -- Constrain Output with WHERE + SELECT + subq_14.listing__view__listing__views + , subq_14.listings + FROM ( + -- Pass Only Elements: ['listings', 'listing__view__listing__views'] + SELECT + subq_13.listing__view__listing__views + , subq_13.listings + FROM ( + -- Join Standard Outputs + SELECT + subq_6.listing AS listing + , subq_12.view__listing AS listing__view__listing + , subq_12.view__listing__views AS listing__view__listing__views + , subq_6.listings AS listings + FROM ( + -- Pass Only Elements: ['listings', 'listing'] + SELECT + subq_5.listing + , subq_5.listings + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.created_at__day + , subq_4.created_at__week + , subq_4.created_at__month + , subq_4.created_at__quarter + , subq_4.created_at__year + , subq_4.created_at__extract_year + , subq_4.created_at__extract_quarter + , subq_4.created_at__extract_month + , subq_4.created_at__extract_day + , subq_4.created_at__extract_dow + , subq_4.created_at__extract_doy + , subq_4.listing__ds__day + , subq_4.listing__ds__week + , subq_4.listing__ds__month + , subq_4.listing__ds__quarter + , subq_4.listing__ds__year + , subq_4.listing__ds__extract_year + , subq_4.listing__ds__extract_quarter + , subq_4.listing__ds__extract_month + , subq_4.listing__ds__extract_day + , subq_4.listing__ds__extract_dow + , subq_4.listing__ds__extract_doy + , subq_4.listing__created_at__day + , subq_4.listing__created_at__week + , subq_4.listing__created_at__month + , subq_4.listing__created_at__quarter + , subq_4.listing__created_at__year + , subq_4.listing__created_at__extract_year + , subq_4.listing__created_at__extract_quarter + , subq_4.listing__created_at__extract_month + , subq_4.listing__created_at__extract_day + , subq_4.listing__created_at__extract_dow + , subq_4.listing__created_at__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.listing + , subq_4.user + , subq_4.listing__user + , subq_4.country_latest + , subq_4.is_lux_latest + , subq_4.capacity_latest + , subq_4.listing__country_latest + , subq_4.listing__is_lux_latest + , subq_4.listing__capacity_latest + , subq_4.listings + , subq_4.largest_listing + , subq_4.smallest_listing + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + SELECT + 1 AS listings + , listings_latest_src_28000.capacity AS largest_listing + , listings_latest_src_28000.capacity AS smallest_listing + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM listings_latest_src_28000.created_at) AS ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS created_at__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM listings_latest_src_28000.created_at) AS created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS created_at__extract_doy + , listings_latest_src_28000.country AS country_latest + , listings_latest_src_28000.is_lux AS is_lux_latest + , listings_latest_src_28000.capacity AS capacity_latest + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM listings_latest_src_28000.created_at) AS listing__ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_doy + , listings_latest_src_28000.country AS listing__country_latest + , listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , listings_latest_src_28000.listing_id AS listing + , listings_latest_src_28000.user_id AS user + , listings_latest_src_28000.user_id AS listing__user + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_4 + ) subq_5 + ) subq_6 + LEFT OUTER JOIN ( + -- Pass Only Elements: ['view__listing', 'view__listing__views'] + SELECT + subq_11.view__listing + , subq_11.view__listing__views + FROM ( + -- Compute Metrics via Expressions + SELECT + subq_10.view__listing + , subq_10.views AS view__listing__views + FROM ( + -- Aggregate Measures + SELECT + subq_9.view__listing + , SUM(subq_9.views) AS views + FROM ( + -- Pass Only Elements: ['views', 'view__listing'] + SELECT + subq_8.view__listing + , subq_8.views + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.ds_partitioned__day + , subq_7.ds_partitioned__week + , subq_7.ds_partitioned__month + , subq_7.ds_partitioned__quarter + , subq_7.ds_partitioned__year + , subq_7.ds_partitioned__extract_year + , subq_7.ds_partitioned__extract_quarter + , subq_7.ds_partitioned__extract_month + , subq_7.ds_partitioned__extract_day + , subq_7.ds_partitioned__extract_dow + , subq_7.ds_partitioned__extract_doy + , subq_7.view__ds__day + , subq_7.view__ds__week + , subq_7.view__ds__month + , subq_7.view__ds__quarter + , subq_7.view__ds__year + , subq_7.view__ds__extract_year + , subq_7.view__ds__extract_quarter + , subq_7.view__ds__extract_month + , subq_7.view__ds__extract_day + , subq_7.view__ds__extract_dow + , subq_7.view__ds__extract_doy + , subq_7.view__ds_partitioned__day + , subq_7.view__ds_partitioned__week + , subq_7.view__ds_partitioned__month + , subq_7.view__ds_partitioned__quarter + , subq_7.view__ds_partitioned__year + , subq_7.view__ds_partitioned__extract_year + , subq_7.view__ds_partitioned__extract_quarter + , subq_7.view__ds_partitioned__extract_month + , subq_7.view__ds_partitioned__extract_day + , subq_7.view__ds_partitioned__extract_dow + , subq_7.view__ds_partitioned__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.listing + , subq_7.user + , subq_7.view__listing + , subq_7.view__user + , subq_7.views + FROM ( + -- Read Elements From Semantic Model 'views_source' + SELECT + 1 AS views + , DATE_TRUNC('day', views_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', views_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', views_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', views_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', views_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM views_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM views_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM views_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM views_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM views_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM views_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', views_source_src_28000.ds_partitioned) AS ds_partitioned__day + , DATE_TRUNC('week', views_source_src_28000.ds_partitioned) AS ds_partitioned__week + , DATE_TRUNC('month', views_source_src_28000.ds_partitioned) AS ds_partitioned__month + , DATE_TRUNC('quarter', views_source_src_28000.ds_partitioned) AS ds_partitioned__quarter + , DATE_TRUNC('year', views_source_src_28000.ds_partitioned) AS ds_partitioned__year + , EXTRACT(year FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_dow + , EXTRACT(doy FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC('day', views_source_src_28000.ds) AS view__ds__day + , DATE_TRUNC('week', views_source_src_28000.ds) AS view__ds__week + , DATE_TRUNC('month', views_source_src_28000.ds) AS view__ds__month + , DATE_TRUNC('quarter', views_source_src_28000.ds) AS view__ds__quarter + , DATE_TRUNC('year', views_source_src_28000.ds) AS view__ds__year + , EXTRACT(year FROM views_source_src_28000.ds) AS view__ds__extract_year + , EXTRACT(quarter FROM views_source_src_28000.ds) AS view__ds__extract_quarter + , EXTRACT(month FROM views_source_src_28000.ds) AS view__ds__extract_month + , EXTRACT(day FROM views_source_src_28000.ds) AS view__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM views_source_src_28000.ds) AS view__ds__extract_dow + , EXTRACT(doy FROM views_source_src_28000.ds) AS view__ds__extract_doy + , DATE_TRUNC('day', views_source_src_28000.ds_partitioned) AS view__ds_partitioned__day + , DATE_TRUNC('week', views_source_src_28000.ds_partitioned) AS view__ds_partitioned__week + , DATE_TRUNC('month', views_source_src_28000.ds_partitioned) AS view__ds_partitioned__month + , DATE_TRUNC('quarter', views_source_src_28000.ds_partitioned) AS view__ds_partitioned__quarter + , DATE_TRUNC('year', views_source_src_28000.ds_partitioned) AS view__ds_partitioned__year + , EXTRACT(year FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_year + , EXTRACT(quarter FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_quarter + , EXTRACT(month FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_month + , EXTRACT(day FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_dow + , EXTRACT(doy FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_doy + , views_source_src_28000.listing_id AS listing + , views_source_src_28000.user_id AS user + , views_source_src_28000.listing_id AS view__listing + , views_source_src_28000.user_id AS view__user + FROM ***************************.fct_views views_source_src_28000 + ) subq_7 + ) subq_8 + ) subq_9 + GROUP BY + subq_9.view__listing + ) subq_10 + ) subq_11 + ) subq_12 + ON + subq_6.listing = subq_12.view__listing + ) subq_13 + ) subq_14 + WHERE listing__view__listing__views > 2 + ) subq_15 + ) subq_16 +) subq_17 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_group_by_has_local_entity_prefix2__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_group_by_has_local_entity_prefix2__plan0_optimized.sql new file mode 100644 index 0000000000..05fd368fbd --- /dev/null +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_group_by_has_local_entity_prefix2__plan0_optimized.sql @@ -0,0 +1,44 @@ +-- Constrain Output with WHERE +-- Pass Only Elements: ['listings',] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + SUM(listings) AS listings +FROM ( + -- Join Standard Outputs + -- Pass Only Elements: ['listings', 'listing__view__listing__views'] + SELECT + subq_26.view__listing__views AS listing__view__listing__views + , subq_20.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['listings', 'listing'] + SELECT + listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN ( + -- Aggregate Measures + -- Compute Metrics via Expressions + -- Pass Only Elements: ['view__listing', 'view__listing__views'] + SELECT + view__listing + , SUM(views) AS view__listing__views + FROM ( + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['views', 'view__listing'] + SELECT + listing_id AS view__listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_23 + GROUP BY + view__listing + ) subq_26 + ON + subq_20.listing = subq_26.view__listing +) subq_28 +WHERE listing__view__listing__views > 2 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_filter_with_conversion_metric__plan0.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_filter_with_conversion_metric__plan0.sql new file mode 100644 index 0000000000..eacffcef29 --- /dev/null +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_filter_with_conversion_metric__plan0.sql @@ -0,0 +1,555 @@ +-- Compute Metrics via Expressions +SELECT + subq_39.listings +FROM ( + -- Aggregate Measures + SELECT + SUM(subq_38.listings) AS listings + FROM ( + -- Pass Only Elements: ['listings',] + SELECT + subq_37.listings + FROM ( + -- Constrain Output with WHERE + SELECT + subq_36.user__visit_buy_conversion_rate + , subq_36.listings + FROM ( + -- Pass Only Elements: ['listings', 'user__visit_buy_conversion_rate'] + SELECT + subq_35.user__visit_buy_conversion_rate + , subq_35.listings + FROM ( + -- Join Standard Outputs + SELECT + subq_17.user AS user + , subq_34.user__visit_buy_conversion_rate AS user__visit_buy_conversion_rate + , subq_17.listings AS listings + FROM ( + -- Pass Only Elements: ['listings', 'user'] + SELECT + subq_16.user + , subq_16.listings + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_15.ds__day + , subq_15.ds__week + , subq_15.ds__month + , subq_15.ds__quarter + , subq_15.ds__year + , subq_15.ds__extract_year + , subq_15.ds__extract_quarter + , subq_15.ds__extract_month + , subq_15.ds__extract_day + , subq_15.ds__extract_dow + , subq_15.ds__extract_doy + , subq_15.created_at__day + , subq_15.created_at__week + , subq_15.created_at__month + , subq_15.created_at__quarter + , subq_15.created_at__year + , subq_15.created_at__extract_year + , subq_15.created_at__extract_quarter + , subq_15.created_at__extract_month + , subq_15.created_at__extract_day + , subq_15.created_at__extract_dow + , subq_15.created_at__extract_doy + , subq_15.listing__ds__day + , subq_15.listing__ds__week + , subq_15.listing__ds__month + , subq_15.listing__ds__quarter + , subq_15.listing__ds__year + , subq_15.listing__ds__extract_year + , subq_15.listing__ds__extract_quarter + , subq_15.listing__ds__extract_month + , subq_15.listing__ds__extract_day + , subq_15.listing__ds__extract_dow + , subq_15.listing__ds__extract_doy + , subq_15.listing__created_at__day + , subq_15.listing__created_at__week + , subq_15.listing__created_at__month + , subq_15.listing__created_at__quarter + , subq_15.listing__created_at__year + , subq_15.listing__created_at__extract_year + , subq_15.listing__created_at__extract_quarter + , subq_15.listing__created_at__extract_month + , subq_15.listing__created_at__extract_day + , subq_15.listing__created_at__extract_dow + , subq_15.listing__created_at__extract_doy + , subq_15.ds__day AS metric_time__day + , subq_15.ds__week AS metric_time__week + , subq_15.ds__month AS metric_time__month + , subq_15.ds__quarter AS metric_time__quarter + , subq_15.ds__year AS metric_time__year + , subq_15.ds__extract_year AS metric_time__extract_year + , subq_15.ds__extract_quarter AS metric_time__extract_quarter + , subq_15.ds__extract_month AS metric_time__extract_month + , subq_15.ds__extract_day AS metric_time__extract_day + , subq_15.ds__extract_dow AS metric_time__extract_dow + , subq_15.ds__extract_doy AS metric_time__extract_doy + , subq_15.listing + , subq_15.user + , subq_15.listing__user + , subq_15.country_latest + , subq_15.is_lux_latest + , subq_15.capacity_latest + , subq_15.listing__country_latest + , subq_15.listing__is_lux_latest + , subq_15.listing__capacity_latest + , subq_15.listings + , subq_15.largest_listing + , subq_15.smallest_listing + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + SELECT + 1 AS listings + , listings_latest_src_28000.capacity AS largest_listing + , listings_latest_src_28000.capacity AS smallest_listing + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS ds__extract_day + , EXTRACT(isodow FROM listings_latest_src_28000.created_at) AS ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS created_at__extract_day + , EXTRACT(isodow FROM listings_latest_src_28000.created_at) AS created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS created_at__extract_doy + , listings_latest_src_28000.country AS country_latest + , listings_latest_src_28000.is_lux AS is_lux_latest + , listings_latest_src_28000.capacity AS capacity_latest + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__ds__extract_day + , EXTRACT(isodow FROM listings_latest_src_28000.created_at) AS listing__ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_day + , EXTRACT(isodow FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_doy + , listings_latest_src_28000.country AS listing__country_latest + , listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , listings_latest_src_28000.listing_id AS listing + , listings_latest_src_28000.user_id AS user + , listings_latest_src_28000.user_id AS listing__user + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_15 + ) subq_16 + ) subq_17 + LEFT OUTER JOIN ( + -- Pass Only Elements: ['user', 'user__visit_buy_conversion_rate'] + SELECT + subq_33.user + , subq_33.user__visit_buy_conversion_rate + FROM ( + -- Compute Metrics via Expressions + SELECT + subq_32.user + , CAST(subq_32.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_32.visits, 0) AS DOUBLE PRECISION) AS user__visit_buy_conversion_rate + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_21.user, subq_31.user) AS user + , MAX(subq_21.visits) AS visits + , MAX(subq_31.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_20.user + , SUM(subq_20.visits) AS visits + FROM ( + -- Pass Only Elements: ['visits', 'user'] + SELECT + subq_19.user + , subq_19.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + 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.visit__ds__day + , subq_18.visit__ds__week + , subq_18.visit__ds__month + , subq_18.visit__ds__quarter + , subq_18.visit__ds__year + , subq_18.visit__ds__extract_year + , subq_18.visit__ds__extract_quarter + , subq_18.visit__ds__extract_month + , subq_18.visit__ds__extract_day + , subq_18.visit__ds__extract_dow + , subq_18.visit__ds__extract_doy + , subq_18.ds__day AS metric_time__day + , subq_18.ds__week AS metric_time__week + , subq_18.ds__month AS metric_time__month + , subq_18.ds__quarter AS metric_time__quarter + , subq_18.ds__year AS metric_time__year + , subq_18.ds__extract_year AS metric_time__extract_year + , subq_18.ds__extract_quarter AS metric_time__extract_quarter + , subq_18.ds__extract_month AS metric_time__extract_month + , subq_18.ds__extract_day AS metric_time__extract_day + , subq_18.ds__extract_dow AS metric_time__extract_dow + , subq_18.ds__extract_doy AS metric_time__extract_doy + , subq_18.user + , subq_18.session + , subq_18.visit__user + , subq_18.visit__session + , subq_18.referrer_id + , subq_18.visit__referrer_id + , subq_18.visits + , subq_18.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_18 + ) subq_19 + ) subq_20 + GROUP BY + subq_20.user + ) subq_21 + FULL OUTER JOIN ( + -- Aggregate Measures + SELECT + subq_30.user + , SUM(subq_30.buys) AS buys + FROM ( + -- Pass Only Elements: ['buys', 'user'] + SELECT + subq_29.user + , subq_29.buys + FROM ( + -- Find conversions for user within the range of INF + SELECT + subq_28.ds__day + , subq_28.user + , subq_28.buys + , subq_28.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_24.visits) OVER ( + PARTITION BY + subq_27.user + , subq_27.ds__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , first_value(subq_24.ds__day) OVER ( + PARTITION BY + subq_27.user + , subq_27.ds__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , first_value(subq_24.user) OVER ( + PARTITION BY + subq_27.user + , subq_27.ds__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_27.mf_internal_uuid AS mf_internal_uuid + , subq_27.buys AS buys + FROM ( + -- Pass Only Elements: ['visits', 'ds__day', 'user'] + SELECT + subq_23.ds__day + , subq_23.user + , subq_23.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_22.ds__day + , subq_22.ds__week + , subq_22.ds__month + , subq_22.ds__quarter + , subq_22.ds__year + , subq_22.ds__extract_year + , subq_22.ds__extract_quarter + , subq_22.ds__extract_month + , subq_22.ds__extract_day + , subq_22.ds__extract_dow + , subq_22.ds__extract_doy + , subq_22.visit__ds__day + , subq_22.visit__ds__week + , subq_22.visit__ds__month + , subq_22.visit__ds__quarter + , subq_22.visit__ds__year + , subq_22.visit__ds__extract_year + , subq_22.visit__ds__extract_quarter + , subq_22.visit__ds__extract_month + , subq_22.visit__ds__extract_day + , subq_22.visit__ds__extract_dow + , subq_22.visit__ds__extract_doy + , subq_22.ds__day AS metric_time__day + , subq_22.ds__week AS metric_time__week + , subq_22.ds__month AS metric_time__month + , subq_22.ds__quarter AS metric_time__quarter + , subq_22.ds__year AS metric_time__year + , subq_22.ds__extract_year AS metric_time__extract_year + , subq_22.ds__extract_quarter AS metric_time__extract_quarter + , subq_22.ds__extract_month AS metric_time__extract_month + , subq_22.ds__extract_day AS metric_time__extract_day + , subq_22.ds__extract_dow AS metric_time__extract_dow + , subq_22.ds__extract_doy AS metric_time__extract_doy + , subq_22.user + , subq_22.session + , subq_22.visit__user + , subq_22.visit__session + , subq_22.referrer_id + , subq_22.visit__referrer_id + , subq_22.visits + , subq_22.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_22 + ) subq_23 + ) subq_24 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_26.ds__day + , subq_26.ds__week + , subq_26.ds__month + , subq_26.ds__quarter + , subq_26.ds__year + , subq_26.ds__extract_year + , subq_26.ds__extract_quarter + , subq_26.ds__extract_month + , subq_26.ds__extract_day + , subq_26.ds__extract_dow + , subq_26.ds__extract_doy + , subq_26.buy__ds__day + , subq_26.buy__ds__week + , subq_26.buy__ds__month + , subq_26.buy__ds__quarter + , subq_26.buy__ds__year + , subq_26.buy__ds__extract_year + , subq_26.buy__ds__extract_quarter + , subq_26.buy__ds__extract_month + , subq_26.buy__ds__extract_day + , subq_26.buy__ds__extract_dow + , subq_26.buy__ds__extract_doy + , subq_26.metric_time__day + , subq_26.metric_time__week + , subq_26.metric_time__month + , subq_26.metric_time__quarter + , subq_26.metric_time__year + , subq_26.metric_time__extract_year + , subq_26.metric_time__extract_quarter + , subq_26.metric_time__extract_month + , subq_26.metric_time__extract_day + , subq_26.metric_time__extract_dow + , subq_26.metric_time__extract_doy + , subq_26.user + , subq_26.session_id + , subq_26.buy__user + , subq_26.buy__session_id + , subq_26.buys + , subq_26.buyers + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_25.ds__day + , subq_25.ds__week + , subq_25.ds__month + , subq_25.ds__quarter + , subq_25.ds__year + , subq_25.ds__extract_year + , subq_25.ds__extract_quarter + , subq_25.ds__extract_month + , subq_25.ds__extract_day + , subq_25.ds__extract_dow + , subq_25.ds__extract_doy + , subq_25.buy__ds__day + , subq_25.buy__ds__week + , subq_25.buy__ds__month + , subq_25.buy__ds__quarter + , subq_25.buy__ds__year + , subq_25.buy__ds__extract_year + , subq_25.buy__ds__extract_quarter + , subq_25.buy__ds__extract_month + , subq_25.buy__ds__extract_day + , subq_25.buy__ds__extract_dow + , subq_25.buy__ds__extract_doy + , subq_25.ds__day AS metric_time__day + , subq_25.ds__week AS metric_time__week + , subq_25.ds__month AS metric_time__month + , subq_25.ds__quarter AS metric_time__quarter + , subq_25.ds__year AS metric_time__year + , subq_25.ds__extract_year AS metric_time__extract_year + , subq_25.ds__extract_quarter AS metric_time__extract_quarter + , subq_25.ds__extract_month AS metric_time__extract_month + , subq_25.ds__extract_day AS metric_time__extract_day + , subq_25.ds__extract_dow AS metric_time__extract_dow + , subq_25.ds__extract_doy AS metric_time__extract_doy + , subq_25.user + , subq_25.session_id + , subq_25.buy__user + , subq_25.buy__session_id + , subq_25.buys + , subq_25.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_25 + ) subq_26 + ) subq_27 + ON + ( + subq_24.user = subq_27.user + ) AND ( + (subq_24.ds__day <= subq_27.ds__day) + ) + ) subq_28 + ) subq_29 + ) subq_30 + GROUP BY + subq_30.user + ) subq_31 + ON + subq_21.user = subq_31.user + GROUP BY + COALESCE(subq_21.user, subq_31.user) + ) subq_32 + ) subq_33 + ) subq_34 + ON + subq_17.user = subq_34.user + ) subq_35 + ) subq_36 + WHERE user__visit_buy_conversion_rate > 2 + ) subq_37 + ) subq_38 +) subq_39 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_filter_with_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_filter_with_conversion_metric__plan0_optimized.sql new file mode 100644 index 0000000000..301829d4fa --- /dev/null +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_filter_with_conversion_metric__plan0_optimized.sql @@ -0,0 +1,120 @@ +-- Constrain Output with WHERE +-- Pass Only Elements: ['listings',] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + SUM(listings) AS listings +FROM ( + -- Join Standard Outputs + -- Pass Only Elements: ['listings', 'user__visit_buy_conversion_rate'] + SELECT + CAST(subq_57.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_57.visits, 0) AS DOUBLE PRECISION) AS user__visit_buy_conversion_rate + , subq_42.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['listings', 'user'] + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_42 + LEFT OUTER JOIN ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_46.user, subq_56.user) AS user + , MAX(subq_46.visits) AS visits + , MAX(subq_56.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_45.user + , SUM(visits) AS visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['visits', 'user'] + SELECT + user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_45 + GROUP BY + subq_45.user + ) subq_46 + FULL OUTER JOIN ( + -- Find conversions for user within the range of INF + -- Pass Only Elements: ['buys', 'user'] + -- Aggregate Measures + SELECT + subq_53.user + , SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_49.visits) OVER ( + PARTITION BY + subq_52.user + , subq_52.ds__day + , subq_52.mf_internal_uuid + ORDER BY subq_49.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , first_value(subq_49.ds__day) OVER ( + PARTITION BY + subq_52.user + , subq_52.ds__day + , subq_52.mf_internal_uuid + ORDER BY subq_49.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , first_value(subq_49.user) OVER ( + PARTITION BY + subq_52.user + , subq_52.ds__day + , subq_52.mf_internal_uuid + ORDER BY subq_49.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_52.mf_internal_uuid AS mf_internal_uuid + , subq_52.buys AS buys + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['visits', 'ds__day', 'user'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_49 + 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_52 + ON + ( + subq_49.user = subq_52.user + ) AND ( + (subq_49.ds__day <= subq_52.ds__day) + ) + ) subq_53 + GROUP BY + subq_53.user + ) subq_56 + ON + subq_46.user = subq_56.user + GROUP BY + COALESCE(subq_46.user, subq_56.user) + ) subq_57 + ON + subq_42.user = subq_57.user +) subq_61 +WHERE user__visit_buy_conversion_rate > 2 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_group_by_has_local_entity_prefix2__plan0.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_group_by_has_local_entity_prefix2__plan0.sql new file mode 100644 index 0000000000..bad0eaba6e --- /dev/null +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_group_by_has_local_entity_prefix2__plan0.sql @@ -0,0 +1,318 @@ +-- Compute Metrics via Expressions +SELECT + subq_17.listings +FROM ( + -- Aggregate Measures + SELECT + SUM(subq_16.listings) AS listings + FROM ( + -- Pass Only Elements: ['listings',] + SELECT + subq_15.listings + FROM ( + -- Constrain Output with WHERE + SELECT + subq_14.listing__view__listing__views + , subq_14.listings + FROM ( + -- Pass Only Elements: ['listings', 'listing__view__listing__views'] + SELECT + subq_13.listing__view__listing__views + , subq_13.listings + FROM ( + -- Join Standard Outputs + SELECT + subq_6.listing AS listing + , subq_12.view__listing AS listing__view__listing + , subq_12.view__listing__views AS listing__view__listing__views + , subq_6.listings AS listings + FROM ( + -- Pass Only Elements: ['listings', 'listing'] + SELECT + subq_5.listing + , subq_5.listings + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.created_at__day + , subq_4.created_at__week + , subq_4.created_at__month + , subq_4.created_at__quarter + , subq_4.created_at__year + , subq_4.created_at__extract_year + , subq_4.created_at__extract_quarter + , subq_4.created_at__extract_month + , subq_4.created_at__extract_day + , subq_4.created_at__extract_dow + , subq_4.created_at__extract_doy + , subq_4.listing__ds__day + , subq_4.listing__ds__week + , subq_4.listing__ds__month + , subq_4.listing__ds__quarter + , subq_4.listing__ds__year + , subq_4.listing__ds__extract_year + , subq_4.listing__ds__extract_quarter + , subq_4.listing__ds__extract_month + , subq_4.listing__ds__extract_day + , subq_4.listing__ds__extract_dow + , subq_4.listing__ds__extract_doy + , subq_4.listing__created_at__day + , subq_4.listing__created_at__week + , subq_4.listing__created_at__month + , subq_4.listing__created_at__quarter + , subq_4.listing__created_at__year + , subq_4.listing__created_at__extract_year + , subq_4.listing__created_at__extract_quarter + , subq_4.listing__created_at__extract_month + , subq_4.listing__created_at__extract_day + , subq_4.listing__created_at__extract_dow + , subq_4.listing__created_at__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.listing + , subq_4.user + , subq_4.listing__user + , subq_4.country_latest + , subq_4.is_lux_latest + , subq_4.capacity_latest + , subq_4.listing__country_latest + , subq_4.listing__is_lux_latest + , subq_4.listing__capacity_latest + , subq_4.listings + , subq_4.largest_listing + , subq_4.smallest_listing + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + SELECT + 1 AS listings + , listings_latest_src_28000.capacity AS largest_listing + , listings_latest_src_28000.capacity AS smallest_listing + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS ds__extract_day + , EXTRACT(isodow FROM listings_latest_src_28000.created_at) AS ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS created_at__extract_day + , EXTRACT(isodow FROM listings_latest_src_28000.created_at) AS created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS created_at__extract_doy + , listings_latest_src_28000.country AS country_latest + , listings_latest_src_28000.is_lux AS is_lux_latest + , listings_latest_src_28000.capacity AS capacity_latest + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__ds__extract_day + , EXTRACT(isodow FROM listings_latest_src_28000.created_at) AS listing__ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_day + , EXTRACT(isodow FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_doy + , listings_latest_src_28000.country AS listing__country_latest + , listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , listings_latest_src_28000.listing_id AS listing + , listings_latest_src_28000.user_id AS user + , listings_latest_src_28000.user_id AS listing__user + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_4 + ) subq_5 + ) subq_6 + LEFT OUTER JOIN ( + -- Pass Only Elements: ['view__listing', 'view__listing__views'] + SELECT + subq_11.view__listing + , subq_11.view__listing__views + FROM ( + -- Compute Metrics via Expressions + SELECT + subq_10.view__listing + , subq_10.views AS view__listing__views + FROM ( + -- Aggregate Measures + SELECT + subq_9.view__listing + , SUM(subq_9.views) AS views + FROM ( + -- Pass Only Elements: ['views', 'view__listing'] + SELECT + subq_8.view__listing + , subq_8.views + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.ds_partitioned__day + , subq_7.ds_partitioned__week + , subq_7.ds_partitioned__month + , subq_7.ds_partitioned__quarter + , subq_7.ds_partitioned__year + , subq_7.ds_partitioned__extract_year + , subq_7.ds_partitioned__extract_quarter + , subq_7.ds_partitioned__extract_month + , subq_7.ds_partitioned__extract_day + , subq_7.ds_partitioned__extract_dow + , subq_7.ds_partitioned__extract_doy + , subq_7.view__ds__day + , subq_7.view__ds__week + , subq_7.view__ds__month + , subq_7.view__ds__quarter + , subq_7.view__ds__year + , subq_7.view__ds__extract_year + , subq_7.view__ds__extract_quarter + , subq_7.view__ds__extract_month + , subq_7.view__ds__extract_day + , subq_7.view__ds__extract_dow + , subq_7.view__ds__extract_doy + , subq_7.view__ds_partitioned__day + , subq_7.view__ds_partitioned__week + , subq_7.view__ds_partitioned__month + , subq_7.view__ds_partitioned__quarter + , subq_7.view__ds_partitioned__year + , subq_7.view__ds_partitioned__extract_year + , subq_7.view__ds_partitioned__extract_quarter + , subq_7.view__ds_partitioned__extract_month + , subq_7.view__ds_partitioned__extract_day + , subq_7.view__ds_partitioned__extract_dow + , subq_7.view__ds_partitioned__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.listing + , subq_7.user + , subq_7.view__listing + , subq_7.view__user + , subq_7.views + FROM ( + -- Read Elements From Semantic Model 'views_source' + SELECT + 1 AS views + , DATE_TRUNC('day', views_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', views_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', views_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', views_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', views_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM views_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM views_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM views_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM views_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM views_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM views_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', views_source_src_28000.ds_partitioned) AS ds_partitioned__day + , DATE_TRUNC('week', views_source_src_28000.ds_partitioned) AS ds_partitioned__week + , DATE_TRUNC('month', views_source_src_28000.ds_partitioned) AS ds_partitioned__month + , DATE_TRUNC('quarter', views_source_src_28000.ds_partitioned) AS ds_partitioned__quarter + , DATE_TRUNC('year', views_source_src_28000.ds_partitioned) AS ds_partitioned__year + , EXTRACT(year FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , EXTRACT(isodow FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_dow + , EXTRACT(doy FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC('day', views_source_src_28000.ds) AS view__ds__day + , DATE_TRUNC('week', views_source_src_28000.ds) AS view__ds__week + , DATE_TRUNC('month', views_source_src_28000.ds) AS view__ds__month + , DATE_TRUNC('quarter', views_source_src_28000.ds) AS view__ds__quarter + , DATE_TRUNC('year', views_source_src_28000.ds) AS view__ds__year + , EXTRACT(year FROM views_source_src_28000.ds) AS view__ds__extract_year + , EXTRACT(quarter FROM views_source_src_28000.ds) AS view__ds__extract_quarter + , EXTRACT(month FROM views_source_src_28000.ds) AS view__ds__extract_month + , EXTRACT(day FROM views_source_src_28000.ds) AS view__ds__extract_day + , EXTRACT(isodow FROM views_source_src_28000.ds) AS view__ds__extract_dow + , EXTRACT(doy FROM views_source_src_28000.ds) AS view__ds__extract_doy + , DATE_TRUNC('day', views_source_src_28000.ds_partitioned) AS view__ds_partitioned__day + , DATE_TRUNC('week', views_source_src_28000.ds_partitioned) AS view__ds_partitioned__week + , DATE_TRUNC('month', views_source_src_28000.ds_partitioned) AS view__ds_partitioned__month + , DATE_TRUNC('quarter', views_source_src_28000.ds_partitioned) AS view__ds_partitioned__quarter + , DATE_TRUNC('year', views_source_src_28000.ds_partitioned) AS view__ds_partitioned__year + , EXTRACT(year FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_year + , EXTRACT(quarter FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_quarter + , EXTRACT(month FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_month + , EXTRACT(day FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_day + , EXTRACT(isodow FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_dow + , EXTRACT(doy FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_doy + , views_source_src_28000.listing_id AS listing + , views_source_src_28000.user_id AS user + , views_source_src_28000.listing_id AS view__listing + , views_source_src_28000.user_id AS view__user + FROM ***************************.fct_views views_source_src_28000 + ) subq_7 + ) subq_8 + ) subq_9 + GROUP BY + subq_9.view__listing + ) subq_10 + ) subq_11 + ) subq_12 + ON + subq_6.listing = subq_12.view__listing + ) subq_13 + ) subq_14 + WHERE listing__view__listing__views > 2 + ) subq_15 + ) subq_16 +) subq_17 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_group_by_has_local_entity_prefix2__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_group_by_has_local_entity_prefix2__plan0_optimized.sql new file mode 100644 index 0000000000..05fd368fbd --- /dev/null +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_group_by_has_local_entity_prefix2__plan0_optimized.sql @@ -0,0 +1,44 @@ +-- Constrain Output with WHERE +-- Pass Only Elements: ['listings',] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + SUM(listings) AS listings +FROM ( + -- Join Standard Outputs + -- Pass Only Elements: ['listings', 'listing__view__listing__views'] + SELECT + subq_26.view__listing__views AS listing__view__listing__views + , subq_20.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['listings', 'listing'] + SELECT + listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN ( + -- Aggregate Measures + -- Compute Metrics via Expressions + -- Pass Only Elements: ['view__listing', 'view__listing__views'] + SELECT + view__listing + , SUM(views) AS view__listing__views + FROM ( + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['views', 'view__listing'] + SELECT + listing_id AS view__listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_23 + GROUP BY + view__listing + ) subq_26 + ON + subq_20.listing = subq_26.view__listing +) subq_28 +WHERE listing__view__listing__views > 2 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_filter_with_conversion_metric__plan0.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_filter_with_conversion_metric__plan0.sql new file mode 100644 index 0000000000..a45ddb8c2c --- /dev/null +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_filter_with_conversion_metric__plan0.sql @@ -0,0 +1,555 @@ +-- Compute Metrics via Expressions +SELECT + subq_39.listings +FROM ( + -- Aggregate Measures + SELECT + SUM(subq_38.listings) AS listings + FROM ( + -- Pass Only Elements: ['listings',] + SELECT + subq_37.listings + FROM ( + -- Constrain Output with WHERE + SELECT + subq_36.user__visit_buy_conversion_rate + , subq_36.listings + FROM ( + -- Pass Only Elements: ['listings', 'user__visit_buy_conversion_rate'] + SELECT + subq_35.user__visit_buy_conversion_rate + , subq_35.listings + FROM ( + -- Join Standard Outputs + SELECT + subq_17.user AS user + , subq_34.user__visit_buy_conversion_rate AS user__visit_buy_conversion_rate + , subq_17.listings AS listings + FROM ( + -- Pass Only Elements: ['listings', 'user'] + SELECT + subq_16.user + , subq_16.listings + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_15.ds__day + , subq_15.ds__week + , subq_15.ds__month + , subq_15.ds__quarter + , subq_15.ds__year + , subq_15.ds__extract_year + , subq_15.ds__extract_quarter + , subq_15.ds__extract_month + , subq_15.ds__extract_day + , subq_15.ds__extract_dow + , subq_15.ds__extract_doy + , subq_15.created_at__day + , subq_15.created_at__week + , subq_15.created_at__month + , subq_15.created_at__quarter + , subq_15.created_at__year + , subq_15.created_at__extract_year + , subq_15.created_at__extract_quarter + , subq_15.created_at__extract_month + , subq_15.created_at__extract_day + , subq_15.created_at__extract_dow + , subq_15.created_at__extract_doy + , subq_15.listing__ds__day + , subq_15.listing__ds__week + , subq_15.listing__ds__month + , subq_15.listing__ds__quarter + , subq_15.listing__ds__year + , subq_15.listing__ds__extract_year + , subq_15.listing__ds__extract_quarter + , subq_15.listing__ds__extract_month + , subq_15.listing__ds__extract_day + , subq_15.listing__ds__extract_dow + , subq_15.listing__ds__extract_doy + , subq_15.listing__created_at__day + , subq_15.listing__created_at__week + , subq_15.listing__created_at__month + , subq_15.listing__created_at__quarter + , subq_15.listing__created_at__year + , subq_15.listing__created_at__extract_year + , subq_15.listing__created_at__extract_quarter + , subq_15.listing__created_at__extract_month + , subq_15.listing__created_at__extract_day + , subq_15.listing__created_at__extract_dow + , subq_15.listing__created_at__extract_doy + , subq_15.ds__day AS metric_time__day + , subq_15.ds__week AS metric_time__week + , subq_15.ds__month AS metric_time__month + , subq_15.ds__quarter AS metric_time__quarter + , subq_15.ds__year AS metric_time__year + , subq_15.ds__extract_year AS metric_time__extract_year + , subq_15.ds__extract_quarter AS metric_time__extract_quarter + , subq_15.ds__extract_month AS metric_time__extract_month + , subq_15.ds__extract_day AS metric_time__extract_day + , subq_15.ds__extract_dow AS metric_time__extract_dow + , subq_15.ds__extract_doy AS metric_time__extract_doy + , subq_15.listing + , subq_15.user + , subq_15.listing__user + , subq_15.country_latest + , subq_15.is_lux_latest + , subq_15.capacity_latest + , subq_15.listing__country_latest + , subq_15.listing__is_lux_latest + , subq_15.listing__capacity_latest + , subq_15.listings + , subq_15.largest_listing + , subq_15.smallest_listing + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + SELECT + 1 AS listings + , listings_latest_src_28000.capacity AS largest_listing + , listings_latest_src_28000.capacity AS smallest_listing + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM listings_latest_src_28000.created_at) = 0 THEN EXTRACT(dow FROM listings_latest_src_28000.created_at) + 7 ELSE EXTRACT(dow FROM listings_latest_src_28000.created_at) END AS ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS created_at__extract_day + , CASE WHEN EXTRACT(dow FROM listings_latest_src_28000.created_at) = 0 THEN EXTRACT(dow FROM listings_latest_src_28000.created_at) + 7 ELSE EXTRACT(dow FROM listings_latest_src_28000.created_at) END AS created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS created_at__extract_doy + , listings_latest_src_28000.country AS country_latest + , listings_latest_src_28000.is_lux AS is_lux_latest + , listings_latest_src_28000.capacity AS capacity_latest + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__ds__extract_day + , CASE WHEN EXTRACT(dow FROM listings_latest_src_28000.created_at) = 0 THEN EXTRACT(dow FROM listings_latest_src_28000.created_at) + 7 ELSE EXTRACT(dow FROM listings_latest_src_28000.created_at) END AS listing__ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_day + , CASE WHEN EXTRACT(dow FROM listings_latest_src_28000.created_at) = 0 THEN EXTRACT(dow FROM listings_latest_src_28000.created_at) + 7 ELSE EXTRACT(dow FROM listings_latest_src_28000.created_at) END AS listing__created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_doy + , listings_latest_src_28000.country AS listing__country_latest + , listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , listings_latest_src_28000.listing_id AS listing + , listings_latest_src_28000.user_id AS user + , listings_latest_src_28000.user_id AS listing__user + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_15 + ) subq_16 + ) subq_17 + LEFT OUTER JOIN ( + -- Pass Only Elements: ['user', 'user__visit_buy_conversion_rate'] + SELECT + subq_33.user + , subq_33.user__visit_buy_conversion_rate + FROM ( + -- Compute Metrics via Expressions + SELECT + subq_32.user + , CAST(subq_32.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_32.visits, 0) AS DOUBLE PRECISION) AS user__visit_buy_conversion_rate + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_21.user, subq_31.user) AS user + , MAX(subq_21.visits) AS visits + , MAX(subq_31.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_20.user + , SUM(subq_20.visits) AS visits + FROM ( + -- Pass Only Elements: ['visits', 'user'] + SELECT + subq_19.user + , subq_19.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + 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.visit__ds__day + , subq_18.visit__ds__week + , subq_18.visit__ds__month + , subq_18.visit__ds__quarter + , subq_18.visit__ds__year + , subq_18.visit__ds__extract_year + , subq_18.visit__ds__extract_quarter + , subq_18.visit__ds__extract_month + , subq_18.visit__ds__extract_day + , subq_18.visit__ds__extract_dow + , subq_18.visit__ds__extract_doy + , subq_18.ds__day AS metric_time__day + , subq_18.ds__week AS metric_time__week + , subq_18.ds__month AS metric_time__month + , subq_18.ds__quarter AS metric_time__quarter + , subq_18.ds__year AS metric_time__year + , subq_18.ds__extract_year AS metric_time__extract_year + , subq_18.ds__extract_quarter AS metric_time__extract_quarter + , subq_18.ds__extract_month AS metric_time__extract_month + , subq_18.ds__extract_day AS metric_time__extract_day + , subq_18.ds__extract_dow AS metric_time__extract_dow + , subq_18.ds__extract_doy AS metric_time__extract_doy + , subq_18.user + , subq_18.session + , subq_18.visit__user + , subq_18.visit__session + , subq_18.referrer_id + , subq_18.visit__referrer_id + , subq_18.visits + , subq_18.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_18 + ) subq_19 + ) subq_20 + GROUP BY + subq_20.user + ) subq_21 + FULL OUTER JOIN ( + -- Aggregate Measures + SELECT + subq_30.user + , SUM(subq_30.buys) AS buys + FROM ( + -- Pass Only Elements: ['buys', 'user'] + SELECT + subq_29.user + , subq_29.buys + FROM ( + -- Find conversions for user within the range of INF + SELECT + subq_28.ds__day + , subq_28.user + , subq_28.buys + , subq_28.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_24.visits) OVER ( + PARTITION BY + subq_27.user + , subq_27.ds__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , first_value(subq_24.ds__day) OVER ( + PARTITION BY + subq_27.user + , subq_27.ds__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , first_value(subq_24.user) OVER ( + PARTITION BY + subq_27.user + , subq_27.ds__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_27.mf_internal_uuid AS mf_internal_uuid + , subq_27.buys AS buys + FROM ( + -- Pass Only Elements: ['visits', 'ds__day', 'user'] + SELECT + subq_23.ds__day + , subq_23.user + , subq_23.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_22.ds__day + , subq_22.ds__week + , subq_22.ds__month + , subq_22.ds__quarter + , subq_22.ds__year + , subq_22.ds__extract_year + , subq_22.ds__extract_quarter + , subq_22.ds__extract_month + , subq_22.ds__extract_day + , subq_22.ds__extract_dow + , subq_22.ds__extract_doy + , subq_22.visit__ds__day + , subq_22.visit__ds__week + , subq_22.visit__ds__month + , subq_22.visit__ds__quarter + , subq_22.visit__ds__year + , subq_22.visit__ds__extract_year + , subq_22.visit__ds__extract_quarter + , subq_22.visit__ds__extract_month + , subq_22.visit__ds__extract_day + , subq_22.visit__ds__extract_dow + , subq_22.visit__ds__extract_doy + , subq_22.ds__day AS metric_time__day + , subq_22.ds__week AS metric_time__week + , subq_22.ds__month AS metric_time__month + , subq_22.ds__quarter AS metric_time__quarter + , subq_22.ds__year AS metric_time__year + , subq_22.ds__extract_year AS metric_time__extract_year + , subq_22.ds__extract_quarter AS metric_time__extract_quarter + , subq_22.ds__extract_month AS metric_time__extract_month + , subq_22.ds__extract_day AS metric_time__extract_day + , subq_22.ds__extract_dow AS metric_time__extract_dow + , subq_22.ds__extract_doy AS metric_time__extract_doy + , subq_22.user + , subq_22.session + , subq_22.visit__user + , subq_22.visit__session + , subq_22.referrer_id + , subq_22.visit__referrer_id + , subq_22.visits + , subq_22.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_22 + ) subq_23 + ) subq_24 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_26.ds__day + , subq_26.ds__week + , subq_26.ds__month + , subq_26.ds__quarter + , subq_26.ds__year + , subq_26.ds__extract_year + , subq_26.ds__extract_quarter + , subq_26.ds__extract_month + , subq_26.ds__extract_day + , subq_26.ds__extract_dow + , subq_26.ds__extract_doy + , subq_26.buy__ds__day + , subq_26.buy__ds__week + , subq_26.buy__ds__month + , subq_26.buy__ds__quarter + , subq_26.buy__ds__year + , subq_26.buy__ds__extract_year + , subq_26.buy__ds__extract_quarter + , subq_26.buy__ds__extract_month + , subq_26.buy__ds__extract_day + , subq_26.buy__ds__extract_dow + , subq_26.buy__ds__extract_doy + , subq_26.metric_time__day + , subq_26.metric_time__week + , subq_26.metric_time__month + , subq_26.metric_time__quarter + , subq_26.metric_time__year + , subq_26.metric_time__extract_year + , subq_26.metric_time__extract_quarter + , subq_26.metric_time__extract_month + , subq_26.metric_time__extract_day + , subq_26.metric_time__extract_dow + , subq_26.metric_time__extract_doy + , subq_26.user + , subq_26.session_id + , subq_26.buy__user + , subq_26.buy__session_id + , subq_26.buys + , subq_26.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_25.ds__day + , subq_25.ds__week + , subq_25.ds__month + , subq_25.ds__quarter + , subq_25.ds__year + , subq_25.ds__extract_year + , subq_25.ds__extract_quarter + , subq_25.ds__extract_month + , subq_25.ds__extract_day + , subq_25.ds__extract_dow + , subq_25.ds__extract_doy + , subq_25.buy__ds__day + , subq_25.buy__ds__week + , subq_25.buy__ds__month + , subq_25.buy__ds__quarter + , subq_25.buy__ds__year + , subq_25.buy__ds__extract_year + , subq_25.buy__ds__extract_quarter + , subq_25.buy__ds__extract_month + , subq_25.buy__ds__extract_day + , subq_25.buy__ds__extract_dow + , subq_25.buy__ds__extract_doy + , subq_25.ds__day AS metric_time__day + , subq_25.ds__week AS metric_time__week + , subq_25.ds__month AS metric_time__month + , subq_25.ds__quarter AS metric_time__quarter + , subq_25.ds__year AS metric_time__year + , subq_25.ds__extract_year AS metric_time__extract_year + , subq_25.ds__extract_quarter AS metric_time__extract_quarter + , subq_25.ds__extract_month AS metric_time__extract_month + , subq_25.ds__extract_day AS metric_time__extract_day + , subq_25.ds__extract_dow AS metric_time__extract_dow + , subq_25.ds__extract_doy AS metric_time__extract_doy + , subq_25.user + , subq_25.session_id + , subq_25.buy__user + , subq_25.buy__session_id + , subq_25.buys + , subq_25.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_25 + ) subq_26 + ) subq_27 + ON + ( + subq_24.user = subq_27.user + ) AND ( + (subq_24.ds__day <= subq_27.ds__day) + ) + ) subq_28 + ) subq_29 + ) subq_30 + GROUP BY + subq_30.user + ) subq_31 + ON + subq_21.user = subq_31.user + GROUP BY + COALESCE(subq_21.user, subq_31.user) + ) subq_32 + ) subq_33 + ) subq_34 + ON + subq_17.user = subq_34.user + ) subq_35 + ) subq_36 + WHERE user__visit_buy_conversion_rate > 2 + ) subq_37 + ) subq_38 +) subq_39 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_filter_with_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_filter_with_conversion_metric__plan0_optimized.sql new file mode 100644 index 0000000000..3156157b2f --- /dev/null +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_filter_with_conversion_metric__plan0_optimized.sql @@ -0,0 +1,120 @@ +-- Constrain Output with WHERE +-- Pass Only Elements: ['listings',] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + SUM(listings) AS listings +FROM ( + -- Join Standard Outputs + -- Pass Only Elements: ['listings', 'user__visit_buy_conversion_rate'] + SELECT + CAST(subq_57.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_57.visits, 0) AS DOUBLE PRECISION) AS user__visit_buy_conversion_rate + , subq_42.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['listings', 'user'] + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_42 + LEFT OUTER JOIN ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_46.user, subq_56.user) AS user + , MAX(subq_46.visits) AS visits + , MAX(subq_56.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_45.user + , SUM(visits) AS visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['visits', 'user'] + SELECT + user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_45 + GROUP BY + subq_45.user + ) subq_46 + FULL OUTER JOIN ( + -- Find conversions for user within the range of INF + -- Pass Only Elements: ['buys', 'user'] + -- Aggregate Measures + SELECT + subq_53.user + , SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_49.visits) OVER ( + PARTITION BY + subq_52.user + , subq_52.ds__day + , subq_52.mf_internal_uuid + ORDER BY subq_49.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , first_value(subq_49.ds__day) OVER ( + PARTITION BY + subq_52.user + , subq_52.ds__day + , subq_52.mf_internal_uuid + ORDER BY subq_49.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , first_value(subq_49.user) OVER ( + PARTITION BY + subq_52.user + , subq_52.ds__day + , subq_52.mf_internal_uuid + ORDER BY subq_49.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_52.mf_internal_uuid AS mf_internal_uuid + , subq_52.buys AS buys + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['visits', 'ds__day', 'user'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_49 + 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_52 + ON + ( + subq_49.user = subq_52.user + ) AND ( + (subq_49.ds__day <= subq_52.ds__day) + ) + ) subq_53 + GROUP BY + subq_53.user + ) subq_56 + ON + subq_46.user = subq_56.user + GROUP BY + COALESCE(subq_46.user, subq_56.user) + ) subq_57 + ON + subq_42.user = subq_57.user +) subq_61 +WHERE user__visit_buy_conversion_rate > 2 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_group_by_has_local_entity_prefix2__plan0.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_group_by_has_local_entity_prefix2__plan0.sql new file mode 100644 index 0000000000..265016f5c8 --- /dev/null +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_group_by_has_local_entity_prefix2__plan0.sql @@ -0,0 +1,318 @@ +-- Compute Metrics via Expressions +SELECT + subq_17.listings +FROM ( + -- Aggregate Measures + SELECT + SUM(subq_16.listings) AS listings + FROM ( + -- Pass Only Elements: ['listings',] + SELECT + subq_15.listings + FROM ( + -- Constrain Output with WHERE + SELECT + subq_14.listing__view__listing__views + , subq_14.listings + FROM ( + -- Pass Only Elements: ['listings', 'listing__view__listing__views'] + SELECT + subq_13.listing__view__listing__views + , subq_13.listings + FROM ( + -- Join Standard Outputs + SELECT + subq_6.listing AS listing + , subq_12.view__listing AS listing__view__listing + , subq_12.view__listing__views AS listing__view__listing__views + , subq_6.listings AS listings + FROM ( + -- Pass Only Elements: ['listings', 'listing'] + SELECT + subq_5.listing + , subq_5.listings + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.created_at__day + , subq_4.created_at__week + , subq_4.created_at__month + , subq_4.created_at__quarter + , subq_4.created_at__year + , subq_4.created_at__extract_year + , subq_4.created_at__extract_quarter + , subq_4.created_at__extract_month + , subq_4.created_at__extract_day + , subq_4.created_at__extract_dow + , subq_4.created_at__extract_doy + , subq_4.listing__ds__day + , subq_4.listing__ds__week + , subq_4.listing__ds__month + , subq_4.listing__ds__quarter + , subq_4.listing__ds__year + , subq_4.listing__ds__extract_year + , subq_4.listing__ds__extract_quarter + , subq_4.listing__ds__extract_month + , subq_4.listing__ds__extract_day + , subq_4.listing__ds__extract_dow + , subq_4.listing__ds__extract_doy + , subq_4.listing__created_at__day + , subq_4.listing__created_at__week + , subq_4.listing__created_at__month + , subq_4.listing__created_at__quarter + , subq_4.listing__created_at__year + , subq_4.listing__created_at__extract_year + , subq_4.listing__created_at__extract_quarter + , subq_4.listing__created_at__extract_month + , subq_4.listing__created_at__extract_day + , subq_4.listing__created_at__extract_dow + , subq_4.listing__created_at__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.listing + , subq_4.user + , subq_4.listing__user + , subq_4.country_latest + , subq_4.is_lux_latest + , subq_4.capacity_latest + , subq_4.listing__country_latest + , subq_4.listing__is_lux_latest + , subq_4.listing__capacity_latest + , subq_4.listings + , subq_4.largest_listing + , subq_4.smallest_listing + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + SELECT + 1 AS listings + , listings_latest_src_28000.capacity AS largest_listing + , listings_latest_src_28000.capacity AS smallest_listing + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM listings_latest_src_28000.created_at) = 0 THEN EXTRACT(dow FROM listings_latest_src_28000.created_at) + 7 ELSE EXTRACT(dow FROM listings_latest_src_28000.created_at) END AS ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS created_at__extract_day + , CASE WHEN EXTRACT(dow FROM listings_latest_src_28000.created_at) = 0 THEN EXTRACT(dow FROM listings_latest_src_28000.created_at) + 7 ELSE EXTRACT(dow FROM listings_latest_src_28000.created_at) END AS created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS created_at__extract_doy + , listings_latest_src_28000.country AS country_latest + , listings_latest_src_28000.is_lux AS is_lux_latest + , listings_latest_src_28000.capacity AS capacity_latest + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__ds__extract_day + , CASE WHEN EXTRACT(dow FROM listings_latest_src_28000.created_at) = 0 THEN EXTRACT(dow FROM listings_latest_src_28000.created_at) + 7 ELSE EXTRACT(dow FROM listings_latest_src_28000.created_at) END AS listing__ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_day + , CASE WHEN EXTRACT(dow FROM listings_latest_src_28000.created_at) = 0 THEN EXTRACT(dow FROM listings_latest_src_28000.created_at) + 7 ELSE EXTRACT(dow FROM listings_latest_src_28000.created_at) END AS listing__created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_doy + , listings_latest_src_28000.country AS listing__country_latest + , listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , listings_latest_src_28000.listing_id AS listing + , listings_latest_src_28000.user_id AS user + , listings_latest_src_28000.user_id AS listing__user + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_4 + ) subq_5 + ) subq_6 + LEFT OUTER JOIN ( + -- Pass Only Elements: ['view__listing', 'view__listing__views'] + SELECT + subq_11.view__listing + , subq_11.view__listing__views + FROM ( + -- Compute Metrics via Expressions + SELECT + subq_10.view__listing + , subq_10.views AS view__listing__views + FROM ( + -- Aggregate Measures + SELECT + subq_9.view__listing + , SUM(subq_9.views) AS views + FROM ( + -- Pass Only Elements: ['views', 'view__listing'] + SELECT + subq_8.view__listing + , subq_8.views + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.ds_partitioned__day + , subq_7.ds_partitioned__week + , subq_7.ds_partitioned__month + , subq_7.ds_partitioned__quarter + , subq_7.ds_partitioned__year + , subq_7.ds_partitioned__extract_year + , subq_7.ds_partitioned__extract_quarter + , subq_7.ds_partitioned__extract_month + , subq_7.ds_partitioned__extract_day + , subq_7.ds_partitioned__extract_dow + , subq_7.ds_partitioned__extract_doy + , subq_7.view__ds__day + , subq_7.view__ds__week + , subq_7.view__ds__month + , subq_7.view__ds__quarter + , subq_7.view__ds__year + , subq_7.view__ds__extract_year + , subq_7.view__ds__extract_quarter + , subq_7.view__ds__extract_month + , subq_7.view__ds__extract_day + , subq_7.view__ds__extract_dow + , subq_7.view__ds__extract_doy + , subq_7.view__ds_partitioned__day + , subq_7.view__ds_partitioned__week + , subq_7.view__ds_partitioned__month + , subq_7.view__ds_partitioned__quarter + , subq_7.view__ds_partitioned__year + , subq_7.view__ds_partitioned__extract_year + , subq_7.view__ds_partitioned__extract_quarter + , subq_7.view__ds_partitioned__extract_month + , subq_7.view__ds_partitioned__extract_day + , subq_7.view__ds_partitioned__extract_dow + , subq_7.view__ds_partitioned__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.listing + , subq_7.user + , subq_7.view__listing + , subq_7.view__user + , subq_7.views + FROM ( + -- Read Elements From Semantic Model 'views_source' + SELECT + 1 AS views + , DATE_TRUNC('day', views_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', views_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', views_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', views_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', views_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM views_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM views_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM views_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM views_source_src_28000.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM views_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM views_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM views_source_src_28000.ds) END AS ds__extract_dow + , EXTRACT(doy FROM views_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', views_source_src_28000.ds_partitioned) AS ds_partitioned__day + , DATE_TRUNC('week', views_source_src_28000.ds_partitioned) AS ds_partitioned__week + , DATE_TRUNC('month', views_source_src_28000.ds_partitioned) AS ds_partitioned__month + , DATE_TRUNC('quarter', views_source_src_28000.ds_partitioned) AS ds_partitioned__quarter + , DATE_TRUNC('year', views_source_src_28000.ds_partitioned) AS ds_partitioned__year + , EXTRACT(year FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , CASE WHEN EXTRACT(dow FROM views_source_src_28000.ds_partitioned) = 0 THEN EXTRACT(dow FROM views_source_src_28000.ds_partitioned) + 7 ELSE EXTRACT(dow FROM views_source_src_28000.ds_partitioned) END AS ds_partitioned__extract_dow + , EXTRACT(doy FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC('day', views_source_src_28000.ds) AS view__ds__day + , DATE_TRUNC('week', views_source_src_28000.ds) AS view__ds__week + , DATE_TRUNC('month', views_source_src_28000.ds) AS view__ds__month + , DATE_TRUNC('quarter', views_source_src_28000.ds) AS view__ds__quarter + , DATE_TRUNC('year', views_source_src_28000.ds) AS view__ds__year + , EXTRACT(year FROM views_source_src_28000.ds) AS view__ds__extract_year + , EXTRACT(quarter FROM views_source_src_28000.ds) AS view__ds__extract_quarter + , EXTRACT(month FROM views_source_src_28000.ds) AS view__ds__extract_month + , EXTRACT(day FROM views_source_src_28000.ds) AS view__ds__extract_day + , CASE WHEN EXTRACT(dow FROM views_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM views_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM views_source_src_28000.ds) END AS view__ds__extract_dow + , EXTRACT(doy FROM views_source_src_28000.ds) AS view__ds__extract_doy + , DATE_TRUNC('day', views_source_src_28000.ds_partitioned) AS view__ds_partitioned__day + , DATE_TRUNC('week', views_source_src_28000.ds_partitioned) AS view__ds_partitioned__week + , DATE_TRUNC('month', views_source_src_28000.ds_partitioned) AS view__ds_partitioned__month + , DATE_TRUNC('quarter', views_source_src_28000.ds_partitioned) AS view__ds_partitioned__quarter + , DATE_TRUNC('year', views_source_src_28000.ds_partitioned) AS view__ds_partitioned__year + , EXTRACT(year FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_year + , EXTRACT(quarter FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_quarter + , EXTRACT(month FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_month + , EXTRACT(day FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_day + , CASE WHEN EXTRACT(dow FROM views_source_src_28000.ds_partitioned) = 0 THEN EXTRACT(dow FROM views_source_src_28000.ds_partitioned) + 7 ELSE EXTRACT(dow FROM views_source_src_28000.ds_partitioned) END AS view__ds_partitioned__extract_dow + , EXTRACT(doy FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_doy + , views_source_src_28000.listing_id AS listing + , views_source_src_28000.user_id AS user + , views_source_src_28000.listing_id AS view__listing + , views_source_src_28000.user_id AS view__user + FROM ***************************.fct_views views_source_src_28000 + ) subq_7 + ) subq_8 + ) subq_9 + GROUP BY + subq_9.view__listing + ) subq_10 + ) subq_11 + ) subq_12 + ON + subq_6.listing = subq_12.view__listing + ) subq_13 + ) subq_14 + WHERE listing__view__listing__views > 2 + ) subq_15 + ) subq_16 +) subq_17 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_group_by_has_local_entity_prefix2__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_group_by_has_local_entity_prefix2__plan0_optimized.sql new file mode 100644 index 0000000000..05fd368fbd --- /dev/null +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_group_by_has_local_entity_prefix2__plan0_optimized.sql @@ -0,0 +1,44 @@ +-- Constrain Output with WHERE +-- Pass Only Elements: ['listings',] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + SUM(listings) AS listings +FROM ( + -- Join Standard Outputs + -- Pass Only Elements: ['listings', 'listing__view__listing__views'] + SELECT + subq_26.view__listing__views AS listing__view__listing__views + , subq_20.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['listings', 'listing'] + SELECT + listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN ( + -- Aggregate Measures + -- Compute Metrics via Expressions + -- Pass Only Elements: ['view__listing', 'view__listing__views'] + SELECT + view__listing + , SUM(views) AS view__listing__views + FROM ( + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['views', 'view__listing'] + SELECT + listing_id AS view__listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_23 + GROUP BY + view__listing + ) subq_26 + ON + subq_20.listing = subq_26.view__listing +) subq_28 +WHERE listing__view__listing__views > 2 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_filter_with_conversion_metric__plan0.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_filter_with_conversion_metric__plan0.sql new file mode 100644 index 0000000000..42675168db --- /dev/null +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_filter_with_conversion_metric__plan0.sql @@ -0,0 +1,555 @@ +-- Compute Metrics via Expressions +SELECT + subq_39.listings +FROM ( + -- Aggregate Measures + SELECT + SUM(subq_38.listings) AS listings + FROM ( + -- Pass Only Elements: ['listings',] + SELECT + subq_37.listings + FROM ( + -- Constrain Output with WHERE + SELECT + subq_36.user__visit_buy_conversion_rate + , subq_36.listings + FROM ( + -- Pass Only Elements: ['listings', 'user__visit_buy_conversion_rate'] + SELECT + subq_35.user__visit_buy_conversion_rate + , subq_35.listings + FROM ( + -- Join Standard Outputs + SELECT + subq_17.user AS user + , subq_34.user__visit_buy_conversion_rate AS user__visit_buy_conversion_rate + , subq_17.listings AS listings + FROM ( + -- Pass Only Elements: ['listings', 'user'] + SELECT + subq_16.user + , subq_16.listings + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_15.ds__day + , subq_15.ds__week + , subq_15.ds__month + , subq_15.ds__quarter + , subq_15.ds__year + , subq_15.ds__extract_year + , subq_15.ds__extract_quarter + , subq_15.ds__extract_month + , subq_15.ds__extract_day + , subq_15.ds__extract_dow + , subq_15.ds__extract_doy + , subq_15.created_at__day + , subq_15.created_at__week + , subq_15.created_at__month + , subq_15.created_at__quarter + , subq_15.created_at__year + , subq_15.created_at__extract_year + , subq_15.created_at__extract_quarter + , subq_15.created_at__extract_month + , subq_15.created_at__extract_day + , subq_15.created_at__extract_dow + , subq_15.created_at__extract_doy + , subq_15.listing__ds__day + , subq_15.listing__ds__week + , subq_15.listing__ds__month + , subq_15.listing__ds__quarter + , subq_15.listing__ds__year + , subq_15.listing__ds__extract_year + , subq_15.listing__ds__extract_quarter + , subq_15.listing__ds__extract_month + , subq_15.listing__ds__extract_day + , subq_15.listing__ds__extract_dow + , subq_15.listing__ds__extract_doy + , subq_15.listing__created_at__day + , subq_15.listing__created_at__week + , subq_15.listing__created_at__month + , subq_15.listing__created_at__quarter + , subq_15.listing__created_at__year + , subq_15.listing__created_at__extract_year + , subq_15.listing__created_at__extract_quarter + , subq_15.listing__created_at__extract_month + , subq_15.listing__created_at__extract_day + , subq_15.listing__created_at__extract_dow + , subq_15.listing__created_at__extract_doy + , subq_15.ds__day AS metric_time__day + , subq_15.ds__week AS metric_time__week + , subq_15.ds__month AS metric_time__month + , subq_15.ds__quarter AS metric_time__quarter + , subq_15.ds__year AS metric_time__year + , subq_15.ds__extract_year AS metric_time__extract_year + , subq_15.ds__extract_quarter AS metric_time__extract_quarter + , subq_15.ds__extract_month AS metric_time__extract_month + , subq_15.ds__extract_day AS metric_time__extract_day + , subq_15.ds__extract_dow AS metric_time__extract_dow + , subq_15.ds__extract_doy AS metric_time__extract_doy + , subq_15.listing + , subq_15.user + , subq_15.listing__user + , subq_15.country_latest + , subq_15.is_lux_latest + , subq_15.capacity_latest + , subq_15.listing__country_latest + , subq_15.listing__is_lux_latest + , subq_15.listing__capacity_latest + , subq_15.listings + , subq_15.largest_listing + , subq_15.smallest_listing + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + SELECT + 1 AS listings + , listings_latest_src_28000.capacity AS largest_listing + , listings_latest_src_28000.capacity AS smallest_listing + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS ds__extract_day + , EXTRACT(dayofweekiso FROM listings_latest_src_28000.created_at) AS ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS created_at__extract_day + , EXTRACT(dayofweekiso FROM listings_latest_src_28000.created_at) AS created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS created_at__extract_doy + , listings_latest_src_28000.country AS country_latest + , listings_latest_src_28000.is_lux AS is_lux_latest + , listings_latest_src_28000.capacity AS capacity_latest + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__ds__extract_day + , EXTRACT(dayofweekiso FROM listings_latest_src_28000.created_at) AS listing__ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_day + , EXTRACT(dayofweekiso FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_doy + , listings_latest_src_28000.country AS listing__country_latest + , listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , listings_latest_src_28000.listing_id AS listing + , listings_latest_src_28000.user_id AS user + , listings_latest_src_28000.user_id AS listing__user + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_15 + ) subq_16 + ) subq_17 + LEFT OUTER JOIN ( + -- Pass Only Elements: ['user', 'user__visit_buy_conversion_rate'] + SELECT + subq_33.user + , subq_33.user__visit_buy_conversion_rate + FROM ( + -- Compute Metrics via Expressions + SELECT + subq_32.user + , CAST(subq_32.buys AS DOUBLE) / CAST(NULLIF(subq_32.visits, 0) AS DOUBLE) AS user__visit_buy_conversion_rate + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_21.user, subq_31.user) AS user + , MAX(subq_21.visits) AS visits + , MAX(subq_31.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_20.user + , SUM(subq_20.visits) AS visits + FROM ( + -- Pass Only Elements: ['visits', 'user'] + SELECT + subq_19.user + , subq_19.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + 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.visit__ds__day + , subq_18.visit__ds__week + , subq_18.visit__ds__month + , subq_18.visit__ds__quarter + , subq_18.visit__ds__year + , subq_18.visit__ds__extract_year + , subq_18.visit__ds__extract_quarter + , subq_18.visit__ds__extract_month + , subq_18.visit__ds__extract_day + , subq_18.visit__ds__extract_dow + , subq_18.visit__ds__extract_doy + , subq_18.ds__day AS metric_time__day + , subq_18.ds__week AS metric_time__week + , subq_18.ds__month AS metric_time__month + , subq_18.ds__quarter AS metric_time__quarter + , subq_18.ds__year AS metric_time__year + , subq_18.ds__extract_year AS metric_time__extract_year + , subq_18.ds__extract_quarter AS metric_time__extract_quarter + , subq_18.ds__extract_month AS metric_time__extract_month + , subq_18.ds__extract_day AS metric_time__extract_day + , subq_18.ds__extract_dow AS metric_time__extract_dow + , subq_18.ds__extract_doy AS metric_time__extract_doy + , subq_18.user + , subq_18.session + , subq_18.visit__user + , subq_18.visit__session + , subq_18.referrer_id + , subq_18.visit__referrer_id + , subq_18.visits + , subq_18.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_18 + ) subq_19 + ) subq_20 + GROUP BY + subq_20.user + ) subq_21 + FULL OUTER JOIN ( + -- Aggregate Measures + SELECT + subq_30.user + , SUM(subq_30.buys) AS buys + FROM ( + -- Pass Only Elements: ['buys', 'user'] + SELECT + subq_29.user + , subq_29.buys + FROM ( + -- Find conversions for user within the range of INF + SELECT + subq_28.ds__day + , subq_28.user + , subq_28.buys + , subq_28.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_24.visits) OVER ( + PARTITION BY + subq_27.user + , subq_27.ds__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , first_value(subq_24.ds__day) OVER ( + PARTITION BY + subq_27.user + , subq_27.ds__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , first_value(subq_24.user) OVER ( + PARTITION BY + subq_27.user + , subq_27.ds__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_27.mf_internal_uuid AS mf_internal_uuid + , subq_27.buys AS buys + FROM ( + -- Pass Only Elements: ['visits', 'ds__day', 'user'] + SELECT + subq_23.ds__day + , subq_23.user + , subq_23.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_22.ds__day + , subq_22.ds__week + , subq_22.ds__month + , subq_22.ds__quarter + , subq_22.ds__year + , subq_22.ds__extract_year + , subq_22.ds__extract_quarter + , subq_22.ds__extract_month + , subq_22.ds__extract_day + , subq_22.ds__extract_dow + , subq_22.ds__extract_doy + , subq_22.visit__ds__day + , subq_22.visit__ds__week + , subq_22.visit__ds__month + , subq_22.visit__ds__quarter + , subq_22.visit__ds__year + , subq_22.visit__ds__extract_year + , subq_22.visit__ds__extract_quarter + , subq_22.visit__ds__extract_month + , subq_22.visit__ds__extract_day + , subq_22.visit__ds__extract_dow + , subq_22.visit__ds__extract_doy + , subq_22.ds__day AS metric_time__day + , subq_22.ds__week AS metric_time__week + , subq_22.ds__month AS metric_time__month + , subq_22.ds__quarter AS metric_time__quarter + , subq_22.ds__year AS metric_time__year + , subq_22.ds__extract_year AS metric_time__extract_year + , subq_22.ds__extract_quarter AS metric_time__extract_quarter + , subq_22.ds__extract_month AS metric_time__extract_month + , subq_22.ds__extract_day AS metric_time__extract_day + , subq_22.ds__extract_dow AS metric_time__extract_dow + , subq_22.ds__extract_doy AS metric_time__extract_doy + , subq_22.user + , subq_22.session + , subq_22.visit__user + , subq_22.visit__session + , subq_22.referrer_id + , subq_22.visit__referrer_id + , subq_22.visits + , subq_22.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_22 + ) subq_23 + ) subq_24 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_26.ds__day + , subq_26.ds__week + , subq_26.ds__month + , subq_26.ds__quarter + , subq_26.ds__year + , subq_26.ds__extract_year + , subq_26.ds__extract_quarter + , subq_26.ds__extract_month + , subq_26.ds__extract_day + , subq_26.ds__extract_dow + , subq_26.ds__extract_doy + , subq_26.buy__ds__day + , subq_26.buy__ds__week + , subq_26.buy__ds__month + , subq_26.buy__ds__quarter + , subq_26.buy__ds__year + , subq_26.buy__ds__extract_year + , subq_26.buy__ds__extract_quarter + , subq_26.buy__ds__extract_month + , subq_26.buy__ds__extract_day + , subq_26.buy__ds__extract_dow + , subq_26.buy__ds__extract_doy + , subq_26.metric_time__day + , subq_26.metric_time__week + , subq_26.metric_time__month + , subq_26.metric_time__quarter + , subq_26.metric_time__year + , subq_26.metric_time__extract_year + , subq_26.metric_time__extract_quarter + , subq_26.metric_time__extract_month + , subq_26.metric_time__extract_day + , subq_26.metric_time__extract_dow + , subq_26.metric_time__extract_doy + , subq_26.user + , subq_26.session_id + , subq_26.buy__user + , subq_26.buy__session_id + , subq_26.buys + , subq_26.buyers + , UUID_STRING() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_25.ds__day + , subq_25.ds__week + , subq_25.ds__month + , subq_25.ds__quarter + , subq_25.ds__year + , subq_25.ds__extract_year + , subq_25.ds__extract_quarter + , subq_25.ds__extract_month + , subq_25.ds__extract_day + , subq_25.ds__extract_dow + , subq_25.ds__extract_doy + , subq_25.buy__ds__day + , subq_25.buy__ds__week + , subq_25.buy__ds__month + , subq_25.buy__ds__quarter + , subq_25.buy__ds__year + , subq_25.buy__ds__extract_year + , subq_25.buy__ds__extract_quarter + , subq_25.buy__ds__extract_month + , subq_25.buy__ds__extract_day + , subq_25.buy__ds__extract_dow + , subq_25.buy__ds__extract_doy + , subq_25.ds__day AS metric_time__day + , subq_25.ds__week AS metric_time__week + , subq_25.ds__month AS metric_time__month + , subq_25.ds__quarter AS metric_time__quarter + , subq_25.ds__year AS metric_time__year + , subq_25.ds__extract_year AS metric_time__extract_year + , subq_25.ds__extract_quarter AS metric_time__extract_quarter + , subq_25.ds__extract_month AS metric_time__extract_month + , subq_25.ds__extract_day AS metric_time__extract_day + , subq_25.ds__extract_dow AS metric_time__extract_dow + , subq_25.ds__extract_doy AS metric_time__extract_doy + , subq_25.user + , subq_25.session_id + , subq_25.buy__user + , subq_25.buy__session_id + , subq_25.buys + , subq_25.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_25 + ) subq_26 + ) subq_27 + ON + ( + subq_24.user = subq_27.user + ) AND ( + (subq_24.ds__day <= subq_27.ds__day) + ) + ) subq_28 + ) subq_29 + ) subq_30 + GROUP BY + subq_30.user + ) subq_31 + ON + subq_21.user = subq_31.user + GROUP BY + COALESCE(subq_21.user, subq_31.user) + ) subq_32 + ) subq_33 + ) subq_34 + ON + subq_17.user = subq_34.user + ) subq_35 + ) subq_36 + WHERE user__visit_buy_conversion_rate > 2 + ) subq_37 + ) subq_38 +) subq_39 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_filter_with_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_filter_with_conversion_metric__plan0_optimized.sql new file mode 100644 index 0000000000..569538fc6a --- /dev/null +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_filter_with_conversion_metric__plan0_optimized.sql @@ -0,0 +1,120 @@ +-- Constrain Output with WHERE +-- Pass Only Elements: ['listings',] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + SUM(listings) AS listings +FROM ( + -- Join Standard Outputs + -- Pass Only Elements: ['listings', 'user__visit_buy_conversion_rate'] + SELECT + CAST(subq_57.buys AS DOUBLE) / CAST(NULLIF(subq_57.visits, 0) AS DOUBLE) AS user__visit_buy_conversion_rate + , subq_42.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['listings', 'user'] + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_42 + LEFT OUTER JOIN ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_46.user, subq_56.user) AS user + , MAX(subq_46.visits) AS visits + , MAX(subq_56.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_45.user + , SUM(visits) AS visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['visits', 'user'] + SELECT + user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_45 + GROUP BY + subq_45.user + ) subq_46 + FULL OUTER JOIN ( + -- Find conversions for user within the range of INF + -- Pass Only Elements: ['buys', 'user'] + -- Aggregate Measures + SELECT + subq_53.user + , SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_49.visits) OVER ( + PARTITION BY + subq_52.user + , subq_52.ds__day + , subq_52.mf_internal_uuid + ORDER BY subq_49.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , first_value(subq_49.ds__day) OVER ( + PARTITION BY + subq_52.user + , subq_52.ds__day + , subq_52.mf_internal_uuid + ORDER BY subq_49.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , first_value(subq_49.user) OVER ( + PARTITION BY + subq_52.user + , subq_52.ds__day + , subq_52.mf_internal_uuid + ORDER BY subq_49.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_52.mf_internal_uuid AS mf_internal_uuid + , subq_52.buys AS buys + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['visits', 'ds__day', 'user'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_49 + 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_52 + ON + ( + subq_49.user = subq_52.user + ) AND ( + (subq_49.ds__day <= subq_52.ds__day) + ) + ) subq_53 + GROUP BY + subq_53.user + ) subq_56 + ON + subq_46.user = subq_56.user + GROUP BY + COALESCE(subq_46.user, subq_56.user) + ) subq_57 + ON + subq_42.user = subq_57.user +) subq_61 +WHERE user__visit_buy_conversion_rate > 2 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_group_by_has_local_entity_prefix2__plan0.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_group_by_has_local_entity_prefix2__plan0.sql new file mode 100644 index 0000000000..466cc2f2ae --- /dev/null +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_group_by_has_local_entity_prefix2__plan0.sql @@ -0,0 +1,318 @@ +-- Compute Metrics via Expressions +SELECT + subq_17.listings +FROM ( + -- Aggregate Measures + SELECT + SUM(subq_16.listings) AS listings + FROM ( + -- Pass Only Elements: ['listings',] + SELECT + subq_15.listings + FROM ( + -- Constrain Output with WHERE + SELECT + subq_14.listing__view__listing__views + , subq_14.listings + FROM ( + -- Pass Only Elements: ['listings', 'listing__view__listing__views'] + SELECT + subq_13.listing__view__listing__views + , subq_13.listings + FROM ( + -- Join Standard Outputs + SELECT + subq_6.listing AS listing + , subq_12.view__listing AS listing__view__listing + , subq_12.view__listing__views AS listing__view__listing__views + , subq_6.listings AS listings + FROM ( + -- Pass Only Elements: ['listings', 'listing'] + SELECT + subq_5.listing + , subq_5.listings + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.created_at__day + , subq_4.created_at__week + , subq_4.created_at__month + , subq_4.created_at__quarter + , subq_4.created_at__year + , subq_4.created_at__extract_year + , subq_4.created_at__extract_quarter + , subq_4.created_at__extract_month + , subq_4.created_at__extract_day + , subq_4.created_at__extract_dow + , subq_4.created_at__extract_doy + , subq_4.listing__ds__day + , subq_4.listing__ds__week + , subq_4.listing__ds__month + , subq_4.listing__ds__quarter + , subq_4.listing__ds__year + , subq_4.listing__ds__extract_year + , subq_4.listing__ds__extract_quarter + , subq_4.listing__ds__extract_month + , subq_4.listing__ds__extract_day + , subq_4.listing__ds__extract_dow + , subq_4.listing__ds__extract_doy + , subq_4.listing__created_at__day + , subq_4.listing__created_at__week + , subq_4.listing__created_at__month + , subq_4.listing__created_at__quarter + , subq_4.listing__created_at__year + , subq_4.listing__created_at__extract_year + , subq_4.listing__created_at__extract_quarter + , subq_4.listing__created_at__extract_month + , subq_4.listing__created_at__extract_day + , subq_4.listing__created_at__extract_dow + , subq_4.listing__created_at__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.listing + , subq_4.user + , subq_4.listing__user + , subq_4.country_latest + , subq_4.is_lux_latest + , subq_4.capacity_latest + , subq_4.listing__country_latest + , subq_4.listing__is_lux_latest + , subq_4.listing__capacity_latest + , subq_4.listings + , subq_4.largest_listing + , subq_4.smallest_listing + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + SELECT + 1 AS listings + , listings_latest_src_28000.capacity AS largest_listing + , listings_latest_src_28000.capacity AS smallest_listing + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS ds__extract_day + , EXTRACT(dayofweekiso FROM listings_latest_src_28000.created_at) AS ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS created_at__extract_day + , EXTRACT(dayofweekiso FROM listings_latest_src_28000.created_at) AS created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS created_at__extract_doy + , listings_latest_src_28000.country AS country_latest + , listings_latest_src_28000.is_lux AS is_lux_latest + , listings_latest_src_28000.capacity AS capacity_latest + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__ds__extract_day + , EXTRACT(dayofweekiso FROM listings_latest_src_28000.created_at) AS listing__ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_day + , EXTRACT(dayofweekiso FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_doy + , listings_latest_src_28000.country AS listing__country_latest + , listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , listings_latest_src_28000.listing_id AS listing + , listings_latest_src_28000.user_id AS user + , listings_latest_src_28000.user_id AS listing__user + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_4 + ) subq_5 + ) subq_6 + LEFT OUTER JOIN ( + -- Pass Only Elements: ['view__listing', 'view__listing__views'] + SELECT + subq_11.view__listing + , subq_11.view__listing__views + FROM ( + -- Compute Metrics via Expressions + SELECT + subq_10.view__listing + , subq_10.views AS view__listing__views + FROM ( + -- Aggregate Measures + SELECT + subq_9.view__listing + , SUM(subq_9.views) AS views + FROM ( + -- Pass Only Elements: ['views', 'view__listing'] + SELECT + subq_8.view__listing + , subq_8.views + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.ds_partitioned__day + , subq_7.ds_partitioned__week + , subq_7.ds_partitioned__month + , subq_7.ds_partitioned__quarter + , subq_7.ds_partitioned__year + , subq_7.ds_partitioned__extract_year + , subq_7.ds_partitioned__extract_quarter + , subq_7.ds_partitioned__extract_month + , subq_7.ds_partitioned__extract_day + , subq_7.ds_partitioned__extract_dow + , subq_7.ds_partitioned__extract_doy + , subq_7.view__ds__day + , subq_7.view__ds__week + , subq_7.view__ds__month + , subq_7.view__ds__quarter + , subq_7.view__ds__year + , subq_7.view__ds__extract_year + , subq_7.view__ds__extract_quarter + , subq_7.view__ds__extract_month + , subq_7.view__ds__extract_day + , subq_7.view__ds__extract_dow + , subq_7.view__ds__extract_doy + , subq_7.view__ds_partitioned__day + , subq_7.view__ds_partitioned__week + , subq_7.view__ds_partitioned__month + , subq_7.view__ds_partitioned__quarter + , subq_7.view__ds_partitioned__year + , subq_7.view__ds_partitioned__extract_year + , subq_7.view__ds_partitioned__extract_quarter + , subq_7.view__ds_partitioned__extract_month + , subq_7.view__ds_partitioned__extract_day + , subq_7.view__ds_partitioned__extract_dow + , subq_7.view__ds_partitioned__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.listing + , subq_7.user + , subq_7.view__listing + , subq_7.view__user + , subq_7.views + FROM ( + -- Read Elements From Semantic Model 'views_source' + SELECT + 1 AS views + , DATE_TRUNC('day', views_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', views_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', views_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', views_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', views_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM views_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM views_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM views_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM views_source_src_28000.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM views_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM views_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', views_source_src_28000.ds_partitioned) AS ds_partitioned__day + , DATE_TRUNC('week', views_source_src_28000.ds_partitioned) AS ds_partitioned__week + , DATE_TRUNC('month', views_source_src_28000.ds_partitioned) AS ds_partitioned__month + , DATE_TRUNC('quarter', views_source_src_28000.ds_partitioned) AS ds_partitioned__quarter + , DATE_TRUNC('year', views_source_src_28000.ds_partitioned) AS ds_partitioned__year + , EXTRACT(year FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , EXTRACT(dayofweekiso FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_dow + , EXTRACT(doy FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC('day', views_source_src_28000.ds) AS view__ds__day + , DATE_TRUNC('week', views_source_src_28000.ds) AS view__ds__week + , DATE_TRUNC('month', views_source_src_28000.ds) AS view__ds__month + , DATE_TRUNC('quarter', views_source_src_28000.ds) AS view__ds__quarter + , DATE_TRUNC('year', views_source_src_28000.ds) AS view__ds__year + , EXTRACT(year FROM views_source_src_28000.ds) AS view__ds__extract_year + , EXTRACT(quarter FROM views_source_src_28000.ds) AS view__ds__extract_quarter + , EXTRACT(month FROM views_source_src_28000.ds) AS view__ds__extract_month + , EXTRACT(day FROM views_source_src_28000.ds) AS view__ds__extract_day + , EXTRACT(dayofweekiso FROM views_source_src_28000.ds) AS view__ds__extract_dow + , EXTRACT(doy FROM views_source_src_28000.ds) AS view__ds__extract_doy + , DATE_TRUNC('day', views_source_src_28000.ds_partitioned) AS view__ds_partitioned__day + , DATE_TRUNC('week', views_source_src_28000.ds_partitioned) AS view__ds_partitioned__week + , DATE_TRUNC('month', views_source_src_28000.ds_partitioned) AS view__ds_partitioned__month + , DATE_TRUNC('quarter', views_source_src_28000.ds_partitioned) AS view__ds_partitioned__quarter + , DATE_TRUNC('year', views_source_src_28000.ds_partitioned) AS view__ds_partitioned__year + , EXTRACT(year FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_year + , EXTRACT(quarter FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_quarter + , EXTRACT(month FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_month + , EXTRACT(day FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_day + , EXTRACT(dayofweekiso FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_dow + , EXTRACT(doy FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_doy + , views_source_src_28000.listing_id AS listing + , views_source_src_28000.user_id AS user + , views_source_src_28000.listing_id AS view__listing + , views_source_src_28000.user_id AS view__user + FROM ***************************.fct_views views_source_src_28000 + ) subq_7 + ) subq_8 + ) subq_9 + GROUP BY + subq_9.view__listing + ) subq_10 + ) subq_11 + ) subq_12 + ON + subq_6.listing = subq_12.view__listing + ) subq_13 + ) subq_14 + WHERE listing__view__listing__views > 2 + ) subq_15 + ) subq_16 +) subq_17 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_group_by_has_local_entity_prefix2__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_group_by_has_local_entity_prefix2__plan0_optimized.sql new file mode 100644 index 0000000000..05fd368fbd --- /dev/null +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_group_by_has_local_entity_prefix2__plan0_optimized.sql @@ -0,0 +1,44 @@ +-- Constrain Output with WHERE +-- Pass Only Elements: ['listings',] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + SUM(listings) AS listings +FROM ( + -- Join Standard Outputs + -- Pass Only Elements: ['listings', 'listing__view__listing__views'] + SELECT + subq_26.view__listing__views AS listing__view__listing__views + , subq_20.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['listings', 'listing'] + SELECT + listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN ( + -- Aggregate Measures + -- Compute Metrics via Expressions + -- Pass Only Elements: ['view__listing', 'view__listing__views'] + SELECT + view__listing + , SUM(views) AS view__listing__views + FROM ( + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['views', 'view__listing'] + SELECT + listing_id AS view__listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_23 + GROUP BY + view__listing + ) subq_26 + ON + subq_20.listing = subq_26.view__listing +) subq_28 +WHERE listing__view__listing__views > 2 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_filter_with_conversion_metric__plan0.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_filter_with_conversion_metric__plan0.sql new file mode 100644 index 0000000000..35aafc5d9a --- /dev/null +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_filter_with_conversion_metric__plan0.sql @@ -0,0 +1,555 @@ +-- Compute Metrics via Expressions +SELECT + subq_39.listings +FROM ( + -- Aggregate Measures + SELECT + SUM(subq_38.listings) AS listings + FROM ( + -- Pass Only Elements: ['listings',] + SELECT + subq_37.listings + FROM ( + -- Constrain Output with WHERE + SELECT + subq_36.user__visit_buy_conversion_rate + , subq_36.listings + FROM ( + -- Pass Only Elements: ['listings', 'user__visit_buy_conversion_rate'] + SELECT + subq_35.user__visit_buy_conversion_rate + , subq_35.listings + FROM ( + -- Join Standard Outputs + SELECT + subq_17.user AS user + , subq_34.user__visit_buy_conversion_rate AS user__visit_buy_conversion_rate + , subq_17.listings AS listings + FROM ( + -- Pass Only Elements: ['listings', 'user'] + SELECT + subq_16.user + , subq_16.listings + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_15.ds__day + , subq_15.ds__week + , subq_15.ds__month + , subq_15.ds__quarter + , subq_15.ds__year + , subq_15.ds__extract_year + , subq_15.ds__extract_quarter + , subq_15.ds__extract_month + , subq_15.ds__extract_day + , subq_15.ds__extract_dow + , subq_15.ds__extract_doy + , subq_15.created_at__day + , subq_15.created_at__week + , subq_15.created_at__month + , subq_15.created_at__quarter + , subq_15.created_at__year + , subq_15.created_at__extract_year + , subq_15.created_at__extract_quarter + , subq_15.created_at__extract_month + , subq_15.created_at__extract_day + , subq_15.created_at__extract_dow + , subq_15.created_at__extract_doy + , subq_15.listing__ds__day + , subq_15.listing__ds__week + , subq_15.listing__ds__month + , subq_15.listing__ds__quarter + , subq_15.listing__ds__year + , subq_15.listing__ds__extract_year + , subq_15.listing__ds__extract_quarter + , subq_15.listing__ds__extract_month + , subq_15.listing__ds__extract_day + , subq_15.listing__ds__extract_dow + , subq_15.listing__ds__extract_doy + , subq_15.listing__created_at__day + , subq_15.listing__created_at__week + , subq_15.listing__created_at__month + , subq_15.listing__created_at__quarter + , subq_15.listing__created_at__year + , subq_15.listing__created_at__extract_year + , subq_15.listing__created_at__extract_quarter + , subq_15.listing__created_at__extract_month + , subq_15.listing__created_at__extract_day + , subq_15.listing__created_at__extract_dow + , subq_15.listing__created_at__extract_doy + , subq_15.ds__day AS metric_time__day + , subq_15.ds__week AS metric_time__week + , subq_15.ds__month AS metric_time__month + , subq_15.ds__quarter AS metric_time__quarter + , subq_15.ds__year AS metric_time__year + , subq_15.ds__extract_year AS metric_time__extract_year + , subq_15.ds__extract_quarter AS metric_time__extract_quarter + , subq_15.ds__extract_month AS metric_time__extract_month + , subq_15.ds__extract_day AS metric_time__extract_day + , subq_15.ds__extract_dow AS metric_time__extract_dow + , subq_15.ds__extract_doy AS metric_time__extract_doy + , subq_15.listing + , subq_15.user + , subq_15.listing__user + , subq_15.country_latest + , subq_15.is_lux_latest + , subq_15.capacity_latest + , subq_15.listing__country_latest + , subq_15.listing__is_lux_latest + , subq_15.listing__capacity_latest + , subq_15.listings + , subq_15.largest_listing + , subq_15.smallest_listing + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + SELECT + 1 AS listings + , listings_latest_src_28000.capacity AS largest_listing + , listings_latest_src_28000.capacity AS smallest_listing + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM listings_latest_src_28000.created_at) AS ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS created_at__extract_day + , EXTRACT(DAY_OF_WEEK FROM listings_latest_src_28000.created_at) AS created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS created_at__extract_doy + , listings_latest_src_28000.country AS country_latest + , listings_latest_src_28000.is_lux AS is_lux_latest + , listings_latest_src_28000.capacity AS capacity_latest + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM listings_latest_src_28000.created_at) AS listing__ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_day + , EXTRACT(DAY_OF_WEEK FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_doy + , listings_latest_src_28000.country AS listing__country_latest + , listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , listings_latest_src_28000.listing_id AS listing + , listings_latest_src_28000.user_id AS user + , listings_latest_src_28000.user_id AS listing__user + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_15 + ) subq_16 + ) subq_17 + LEFT OUTER JOIN ( + -- Pass Only Elements: ['user', 'user__visit_buy_conversion_rate'] + SELECT + subq_33.user + , subq_33.user__visit_buy_conversion_rate + FROM ( + -- Compute Metrics via Expressions + SELECT + subq_32.user + , CAST(subq_32.buys AS DOUBLE) / CAST(NULLIF(subq_32.visits, 0) AS DOUBLE) AS user__visit_buy_conversion_rate + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_21.user, subq_31.user) AS user + , MAX(subq_21.visits) AS visits + , MAX(subq_31.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_20.user + , SUM(subq_20.visits) AS visits + FROM ( + -- Pass Only Elements: ['visits', 'user'] + SELECT + subq_19.user + , subq_19.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + 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.visit__ds__day + , subq_18.visit__ds__week + , subq_18.visit__ds__month + , subq_18.visit__ds__quarter + , subq_18.visit__ds__year + , subq_18.visit__ds__extract_year + , subq_18.visit__ds__extract_quarter + , subq_18.visit__ds__extract_month + , subq_18.visit__ds__extract_day + , subq_18.visit__ds__extract_dow + , subq_18.visit__ds__extract_doy + , subq_18.ds__day AS metric_time__day + , subq_18.ds__week AS metric_time__week + , subq_18.ds__month AS metric_time__month + , subq_18.ds__quarter AS metric_time__quarter + , subq_18.ds__year AS metric_time__year + , subq_18.ds__extract_year AS metric_time__extract_year + , subq_18.ds__extract_quarter AS metric_time__extract_quarter + , subq_18.ds__extract_month AS metric_time__extract_month + , subq_18.ds__extract_day AS metric_time__extract_day + , subq_18.ds__extract_dow AS metric_time__extract_dow + , subq_18.ds__extract_doy AS metric_time__extract_doy + , subq_18.user + , subq_18.session + , subq_18.visit__user + , subq_18.visit__session + , subq_18.referrer_id + , subq_18.visit__referrer_id + , subq_18.visits + , subq_18.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_18 + ) subq_19 + ) subq_20 + GROUP BY + subq_20.user + ) subq_21 + FULL OUTER JOIN ( + -- Aggregate Measures + SELECT + subq_30.user + , SUM(subq_30.buys) AS buys + FROM ( + -- Pass Only Elements: ['buys', 'user'] + SELECT + subq_29.user + , subq_29.buys + FROM ( + -- Find conversions for user within the range of INF + SELECT + subq_28.ds__day + , subq_28.user + , subq_28.buys + , subq_28.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_24.visits) OVER ( + PARTITION BY + subq_27.user + , subq_27.ds__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , first_value(subq_24.ds__day) OVER ( + PARTITION BY + subq_27.user + , subq_27.ds__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , first_value(subq_24.user) OVER ( + PARTITION BY + subq_27.user + , subq_27.ds__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_27.mf_internal_uuid AS mf_internal_uuid + , subq_27.buys AS buys + FROM ( + -- Pass Only Elements: ['visits', 'ds__day', 'user'] + SELECT + subq_23.ds__day + , subq_23.user + , subq_23.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_22.ds__day + , subq_22.ds__week + , subq_22.ds__month + , subq_22.ds__quarter + , subq_22.ds__year + , subq_22.ds__extract_year + , subq_22.ds__extract_quarter + , subq_22.ds__extract_month + , subq_22.ds__extract_day + , subq_22.ds__extract_dow + , subq_22.ds__extract_doy + , subq_22.visit__ds__day + , subq_22.visit__ds__week + , subq_22.visit__ds__month + , subq_22.visit__ds__quarter + , subq_22.visit__ds__year + , subq_22.visit__ds__extract_year + , subq_22.visit__ds__extract_quarter + , subq_22.visit__ds__extract_month + , subq_22.visit__ds__extract_day + , subq_22.visit__ds__extract_dow + , subq_22.visit__ds__extract_doy + , subq_22.ds__day AS metric_time__day + , subq_22.ds__week AS metric_time__week + , subq_22.ds__month AS metric_time__month + , subq_22.ds__quarter AS metric_time__quarter + , subq_22.ds__year AS metric_time__year + , subq_22.ds__extract_year AS metric_time__extract_year + , subq_22.ds__extract_quarter AS metric_time__extract_quarter + , subq_22.ds__extract_month AS metric_time__extract_month + , subq_22.ds__extract_day AS metric_time__extract_day + , subq_22.ds__extract_dow AS metric_time__extract_dow + , subq_22.ds__extract_doy AS metric_time__extract_doy + , subq_22.user + , subq_22.session + , subq_22.visit__user + , subq_22.visit__session + , subq_22.referrer_id + , subq_22.visit__referrer_id + , subq_22.visits + , subq_22.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_22 + ) subq_23 + ) subq_24 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_26.ds__day + , subq_26.ds__week + , subq_26.ds__month + , subq_26.ds__quarter + , subq_26.ds__year + , subq_26.ds__extract_year + , subq_26.ds__extract_quarter + , subq_26.ds__extract_month + , subq_26.ds__extract_day + , subq_26.ds__extract_dow + , subq_26.ds__extract_doy + , subq_26.buy__ds__day + , subq_26.buy__ds__week + , subq_26.buy__ds__month + , subq_26.buy__ds__quarter + , subq_26.buy__ds__year + , subq_26.buy__ds__extract_year + , subq_26.buy__ds__extract_quarter + , subq_26.buy__ds__extract_month + , subq_26.buy__ds__extract_day + , subq_26.buy__ds__extract_dow + , subq_26.buy__ds__extract_doy + , subq_26.metric_time__day + , subq_26.metric_time__week + , subq_26.metric_time__month + , subq_26.metric_time__quarter + , subq_26.metric_time__year + , subq_26.metric_time__extract_year + , subq_26.metric_time__extract_quarter + , subq_26.metric_time__extract_month + , subq_26.metric_time__extract_day + , subq_26.metric_time__extract_dow + , subq_26.metric_time__extract_doy + , subq_26.user + , subq_26.session_id + , subq_26.buy__user + , subq_26.buy__session_id + , subq_26.buys + , subq_26.buyers + , uuid() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_25.ds__day + , subq_25.ds__week + , subq_25.ds__month + , subq_25.ds__quarter + , subq_25.ds__year + , subq_25.ds__extract_year + , subq_25.ds__extract_quarter + , subq_25.ds__extract_month + , subq_25.ds__extract_day + , subq_25.ds__extract_dow + , subq_25.ds__extract_doy + , subq_25.buy__ds__day + , subq_25.buy__ds__week + , subq_25.buy__ds__month + , subq_25.buy__ds__quarter + , subq_25.buy__ds__year + , subq_25.buy__ds__extract_year + , subq_25.buy__ds__extract_quarter + , subq_25.buy__ds__extract_month + , subq_25.buy__ds__extract_day + , subq_25.buy__ds__extract_dow + , subq_25.buy__ds__extract_doy + , subq_25.ds__day AS metric_time__day + , subq_25.ds__week AS metric_time__week + , subq_25.ds__month AS metric_time__month + , subq_25.ds__quarter AS metric_time__quarter + , subq_25.ds__year AS metric_time__year + , subq_25.ds__extract_year AS metric_time__extract_year + , subq_25.ds__extract_quarter AS metric_time__extract_quarter + , subq_25.ds__extract_month AS metric_time__extract_month + , subq_25.ds__extract_day AS metric_time__extract_day + , subq_25.ds__extract_dow AS metric_time__extract_dow + , subq_25.ds__extract_doy AS metric_time__extract_doy + , subq_25.user + , subq_25.session_id + , subq_25.buy__user + , subq_25.buy__session_id + , subq_25.buys + , subq_25.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_25 + ) subq_26 + ) subq_27 + ON + ( + subq_24.user = subq_27.user + ) AND ( + (subq_24.ds__day <= subq_27.ds__day) + ) + ) subq_28 + ) subq_29 + ) subq_30 + GROUP BY + subq_30.user + ) subq_31 + ON + subq_21.user = subq_31.user + GROUP BY + COALESCE(subq_21.user, subq_31.user) + ) subq_32 + ) subq_33 + ) subq_34 + ON + subq_17.user = subq_34.user + ) subq_35 + ) subq_36 + WHERE user__visit_buy_conversion_rate > 2 + ) subq_37 + ) subq_38 +) subq_39 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_filter_with_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_filter_with_conversion_metric__plan0_optimized.sql new file mode 100644 index 0000000000..33bbe3d966 --- /dev/null +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_filter_with_conversion_metric__plan0_optimized.sql @@ -0,0 +1,120 @@ +-- Constrain Output with WHERE +-- Pass Only Elements: ['listings',] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + SUM(listings) AS listings +FROM ( + -- Join Standard Outputs + -- Pass Only Elements: ['listings', 'user__visit_buy_conversion_rate'] + SELECT + CAST(subq_57.buys AS DOUBLE) / CAST(NULLIF(subq_57.visits, 0) AS DOUBLE) AS user__visit_buy_conversion_rate + , subq_42.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['listings', 'user'] + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_42 + LEFT OUTER JOIN ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_46.user, subq_56.user) AS user + , MAX(subq_46.visits) AS visits + , MAX(subq_56.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_45.user + , SUM(visits) AS visits + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['visits', 'user'] + SELECT + user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_45 + GROUP BY + subq_45.user + ) subq_46 + FULL OUTER JOIN ( + -- Find conversions for user within the range of INF + -- Pass Only Elements: ['buys', 'user'] + -- Aggregate Measures + SELECT + subq_53.user + , SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_49.visits) OVER ( + PARTITION BY + subq_52.user + , subq_52.ds__day + , subq_52.mf_internal_uuid + ORDER BY subq_49.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS visits + , first_value(subq_49.ds__day) OVER ( + PARTITION BY + subq_52.user + , subq_52.ds__day + , subq_52.mf_internal_uuid + ORDER BY subq_49.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__day + , first_value(subq_49.user) OVER ( + PARTITION BY + subq_52.user + , subq_52.ds__day + , subq_52.mf_internal_uuid + ORDER BY subq_49.ds__day DESC + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS user + , subq_52.mf_internal_uuid AS mf_internal_uuid + , subq_52.buys AS buys + FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['visits', 'ds__day', 'user'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + ) subq_49 + 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_52 + ON + ( + subq_49.user = subq_52.user + ) AND ( + (subq_49.ds__day <= subq_52.ds__day) + ) + ) subq_53 + GROUP BY + subq_53.user + ) subq_56 + ON + subq_46.user = subq_56.user + GROUP BY + COALESCE(subq_46.user, subq_56.user) + ) subq_57 + ON + subq_42.user = subq_57.user +) subq_61 +WHERE user__visit_buy_conversion_rate > 2 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_group_by_has_local_entity_prefix2__plan0.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_group_by_has_local_entity_prefix2__plan0.sql new file mode 100644 index 0000000000..15a6d447b3 --- /dev/null +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_group_by_has_local_entity_prefix2__plan0.sql @@ -0,0 +1,318 @@ +-- Compute Metrics via Expressions +SELECT + subq_17.listings +FROM ( + -- Aggregate Measures + SELECT + SUM(subq_16.listings) AS listings + FROM ( + -- Pass Only Elements: ['listings',] + SELECT + subq_15.listings + FROM ( + -- Constrain Output with WHERE + SELECT + subq_14.listing__view__listing__views + , subq_14.listings + FROM ( + -- Pass Only Elements: ['listings', 'listing__view__listing__views'] + SELECT + subq_13.listing__view__listing__views + , subq_13.listings + FROM ( + -- Join Standard Outputs + SELECT + subq_6.listing AS listing + , subq_12.view__listing AS listing__view__listing + , subq_12.view__listing__views AS listing__view__listing__views + , subq_6.listings AS listings + FROM ( + -- Pass Only Elements: ['listings', 'listing'] + SELECT + subq_5.listing + , subq_5.listings + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.created_at__day + , subq_4.created_at__week + , subq_4.created_at__month + , subq_4.created_at__quarter + , subq_4.created_at__year + , subq_4.created_at__extract_year + , subq_4.created_at__extract_quarter + , subq_4.created_at__extract_month + , subq_4.created_at__extract_day + , subq_4.created_at__extract_dow + , subq_4.created_at__extract_doy + , subq_4.listing__ds__day + , subq_4.listing__ds__week + , subq_4.listing__ds__month + , subq_4.listing__ds__quarter + , subq_4.listing__ds__year + , subq_4.listing__ds__extract_year + , subq_4.listing__ds__extract_quarter + , subq_4.listing__ds__extract_month + , subq_4.listing__ds__extract_day + , subq_4.listing__ds__extract_dow + , subq_4.listing__ds__extract_doy + , subq_4.listing__created_at__day + , subq_4.listing__created_at__week + , subq_4.listing__created_at__month + , subq_4.listing__created_at__quarter + , subq_4.listing__created_at__year + , subq_4.listing__created_at__extract_year + , subq_4.listing__created_at__extract_quarter + , subq_4.listing__created_at__extract_month + , subq_4.listing__created_at__extract_day + , subq_4.listing__created_at__extract_dow + , subq_4.listing__created_at__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.listing + , subq_4.user + , subq_4.listing__user + , subq_4.country_latest + , subq_4.is_lux_latest + , subq_4.capacity_latest + , subq_4.listing__country_latest + , subq_4.listing__is_lux_latest + , subq_4.listing__capacity_latest + , subq_4.listings + , subq_4.largest_listing + , subq_4.smallest_listing + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + SELECT + 1 AS listings + , listings_latest_src_28000.capacity AS largest_listing + , listings_latest_src_28000.capacity AS smallest_listing + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM listings_latest_src_28000.created_at) AS ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS created_at__extract_day + , EXTRACT(DAY_OF_WEEK FROM listings_latest_src_28000.created_at) AS created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS created_at__extract_doy + , listings_latest_src_28000.country AS country_latest + , listings_latest_src_28000.is_lux AS is_lux_latest + , listings_latest_src_28000.capacity AS capacity_latest + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM listings_latest_src_28000.created_at) AS listing__ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_day + , EXTRACT(DAY_OF_WEEK FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_doy + , listings_latest_src_28000.country AS listing__country_latest + , listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , listings_latest_src_28000.listing_id AS listing + , listings_latest_src_28000.user_id AS user + , listings_latest_src_28000.user_id AS listing__user + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_4 + ) subq_5 + ) subq_6 + LEFT OUTER JOIN ( + -- Pass Only Elements: ['view__listing', 'view__listing__views'] + SELECT + subq_11.view__listing + , subq_11.view__listing__views + FROM ( + -- Compute Metrics via Expressions + SELECT + subq_10.view__listing + , subq_10.views AS view__listing__views + FROM ( + -- Aggregate Measures + SELECT + subq_9.view__listing + , SUM(subq_9.views) AS views + FROM ( + -- Pass Only Elements: ['views', 'view__listing'] + SELECT + subq_8.view__listing + , subq_8.views + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.ds_partitioned__day + , subq_7.ds_partitioned__week + , subq_7.ds_partitioned__month + , subq_7.ds_partitioned__quarter + , subq_7.ds_partitioned__year + , subq_7.ds_partitioned__extract_year + , subq_7.ds_partitioned__extract_quarter + , subq_7.ds_partitioned__extract_month + , subq_7.ds_partitioned__extract_day + , subq_7.ds_partitioned__extract_dow + , subq_7.ds_partitioned__extract_doy + , subq_7.view__ds__day + , subq_7.view__ds__week + , subq_7.view__ds__month + , subq_7.view__ds__quarter + , subq_7.view__ds__year + , subq_7.view__ds__extract_year + , subq_7.view__ds__extract_quarter + , subq_7.view__ds__extract_month + , subq_7.view__ds__extract_day + , subq_7.view__ds__extract_dow + , subq_7.view__ds__extract_doy + , subq_7.view__ds_partitioned__day + , subq_7.view__ds_partitioned__week + , subq_7.view__ds_partitioned__month + , subq_7.view__ds_partitioned__quarter + , subq_7.view__ds_partitioned__year + , subq_7.view__ds_partitioned__extract_year + , subq_7.view__ds_partitioned__extract_quarter + , subq_7.view__ds_partitioned__extract_month + , subq_7.view__ds_partitioned__extract_day + , subq_7.view__ds_partitioned__extract_dow + , subq_7.view__ds_partitioned__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.listing + , subq_7.user + , subq_7.view__listing + , subq_7.view__user + , subq_7.views + FROM ( + -- Read Elements From Semantic Model 'views_source' + SELECT + 1 AS views + , DATE_TRUNC('day', views_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', views_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', views_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', views_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', views_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM views_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM views_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM views_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM views_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM views_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM views_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', views_source_src_28000.ds_partitioned) AS ds_partitioned__day + , DATE_TRUNC('week', views_source_src_28000.ds_partitioned) AS ds_partitioned__week + , DATE_TRUNC('month', views_source_src_28000.ds_partitioned) AS ds_partitioned__month + , DATE_TRUNC('quarter', views_source_src_28000.ds_partitioned) AS ds_partitioned__quarter + , DATE_TRUNC('year', views_source_src_28000.ds_partitioned) AS ds_partitioned__year + , EXTRACT(year FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , EXTRACT(DAY_OF_WEEK FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_dow + , EXTRACT(doy FROM views_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC('day', views_source_src_28000.ds) AS view__ds__day + , DATE_TRUNC('week', views_source_src_28000.ds) AS view__ds__week + , DATE_TRUNC('month', views_source_src_28000.ds) AS view__ds__month + , DATE_TRUNC('quarter', views_source_src_28000.ds) AS view__ds__quarter + , DATE_TRUNC('year', views_source_src_28000.ds) AS view__ds__year + , EXTRACT(year FROM views_source_src_28000.ds) AS view__ds__extract_year + , EXTRACT(quarter FROM views_source_src_28000.ds) AS view__ds__extract_quarter + , EXTRACT(month FROM views_source_src_28000.ds) AS view__ds__extract_month + , EXTRACT(day FROM views_source_src_28000.ds) AS view__ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM views_source_src_28000.ds) AS view__ds__extract_dow + , EXTRACT(doy FROM views_source_src_28000.ds) AS view__ds__extract_doy + , DATE_TRUNC('day', views_source_src_28000.ds_partitioned) AS view__ds_partitioned__day + , DATE_TRUNC('week', views_source_src_28000.ds_partitioned) AS view__ds_partitioned__week + , DATE_TRUNC('month', views_source_src_28000.ds_partitioned) AS view__ds_partitioned__month + , DATE_TRUNC('quarter', views_source_src_28000.ds_partitioned) AS view__ds_partitioned__quarter + , DATE_TRUNC('year', views_source_src_28000.ds_partitioned) AS view__ds_partitioned__year + , EXTRACT(year FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_year + , EXTRACT(quarter FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_quarter + , EXTRACT(month FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_month + , EXTRACT(day FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_day + , EXTRACT(DAY_OF_WEEK FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_dow + , EXTRACT(doy FROM views_source_src_28000.ds_partitioned) AS view__ds_partitioned__extract_doy + , views_source_src_28000.listing_id AS listing + , views_source_src_28000.user_id AS user + , views_source_src_28000.listing_id AS view__listing + , views_source_src_28000.user_id AS view__user + FROM ***************************.fct_views views_source_src_28000 + ) subq_7 + ) subq_8 + ) subq_9 + GROUP BY + subq_9.view__listing + ) subq_10 + ) subq_11 + ) subq_12 + ON + subq_6.listing = subq_12.view__listing + ) subq_13 + ) subq_14 + WHERE listing__view__listing__views > 2 + ) subq_15 + ) subq_16 +) subq_17 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_group_by_has_local_entity_prefix2__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_group_by_has_local_entity_prefix2__plan0_optimized.sql new file mode 100644 index 0000000000..05fd368fbd --- /dev/null +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_group_by_has_local_entity_prefix2__plan0_optimized.sql @@ -0,0 +1,44 @@ +-- Constrain Output with WHERE +-- Pass Only Elements: ['listings',] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + SUM(listings) AS listings +FROM ( + -- Join Standard Outputs + -- Pass Only Elements: ['listings', 'listing__view__listing__views'] + SELECT + subq_26.view__listing__views AS listing__view__listing__views + , subq_20.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['listings', 'listing'] + SELECT + listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN ( + -- Aggregate Measures + -- Compute Metrics via Expressions + -- Pass Only Elements: ['view__listing', 'view__listing__views'] + SELECT + view__listing + , SUM(views) AS view__listing__views + FROM ( + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['views', 'view__listing'] + SELECT + listing_id AS view__listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_23 + GROUP BY + view__listing + ) subq_26 + ON + subq_20.listing = subq_26.view__listing +) subq_28 +WHERE listing__view__listing__views > 2