From 144f1b4cad9364409b3da340a66fef2f7a91dba0 Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Tue, 12 Nov 2024 22:29:46 -0800 Subject: [PATCH] Update snapshots for other SQL engines. --- ...th_non_default_grains__plan0_optimized.sql | 84 +++--- ...th_non_default_grains__plan0_optimized.sql | 84 +++--- ...th_non_default_grains__plan0_optimized.sql | 84 +++--- ...th_non_default_grains__plan0_optimized.sql | 84 +++--- ...th_non_default_grains__plan0_optimized.sql | 84 +++--- ...th_non_default_grains__plan0_optimized.sql | 84 +++--- ...th_custom_granularity__plan0_optimized.sql | 29 +- ...th_custom_granularity__plan0_optimized.sql | 23 +- ...th_custom_granularity__plan0_optimized.sql | 28 +- ...ilter_not_in_group_by__plan0_optimized.sql | 28 +- ...th_custom_granularity__plan0_optimized.sql | 29 +- ...th_custom_granularity__plan0_optimized.sql | 25 +- ...th_custom_granularity__plan0_optimized.sql | 28 +- ...ilter_not_in_group_by__plan0_optimized.sql | 28 +- ...th_custom_granularity__plan0_optimized.sql | 29 +- ...th_custom_granularity__plan0_optimized.sql | 25 +- ...th_custom_granularity__plan0_optimized.sql | 28 +- ...ilter_not_in_group_by__plan0_optimized.sql | 28 +- ...th_custom_granularity__plan0_optimized.sql | 29 +- ...th_custom_granularity__plan0_optimized.sql | 25 +- ...th_custom_granularity__plan0_optimized.sql | 28 +- ...ilter_not_in_group_by__plan0_optimized.sql | 28 +- ...th_custom_granularity__plan0_optimized.sql | 29 +- ...th_custom_granularity__plan0_optimized.sql | 25 +- ...th_custom_granularity__plan0_optimized.sql | 28 +- ...ilter_not_in_group_by__plan0_optimized.sql | 28 +- ...th_custom_granularity__plan0_optimized.sql | 29 +- ...th_custom_granularity__plan0_optimized.sql | 25 +- ...th_custom_granularity__plan0_optimized.sql | 28 +- ...ilter_not_in_group_by__plan0_optimized.sql | 28 +- .../test_combined_metrics_plan__ep_0.xml | 162 ++++++----- ...test_small_combined_metrics_plan__ep_0.xml | 23 +- .../test_combined_metrics_plan__ep_0.xml | 162 ++++++----- ...test_small_combined_metrics_plan__ep_0.xml | 25 +- .../test_combined_metrics_plan__ep_0.xml | 162 ++++++----- ...test_small_combined_metrics_plan__ep_0.xml | 25 +- .../test_combined_metrics_plan__ep_0.xml | 162 ++++++----- ...test_small_combined_metrics_plan__ep_0.xml | 25 +- .../test_combined_metrics_plan__ep_0.xml | 162 ++++++----- ...test_small_combined_metrics_plan__ep_0.xml | 25 +- .../test_combined_metrics_plan__ep_0.xml | 162 ++++++----- ...test_small_combined_metrics_plan__ep_0.xml | 25 +- ...tiple_semantic_models__plan0_optimized.sql | 146 +++++----- ...tiple_semantic_models__plan0_optimized.sql | 150 +++++----- ...tiple_semantic_models__plan0_optimized.sql | 150 +++++----- ...tiple_semantic_models__plan0_optimized.sql | 150 +++++----- ...tiple_semantic_models__plan0_optimized.sql | 150 +++++----- ...tiple_semantic_models__plan0_optimized.sql | 150 +++++----- ..._with_time_constraint__plan0_optimized.sql | 28 +- .../test_derived_metric__plan0_optimized.sql | 29 +- ..._different_components__plan0_optimized.sql | 95 ++++--- ...ion_and_offset_window__plan0_optimized.sql | 28 +- ..._with_offset_to_grain__plan0_optimized.sql | 109 ++++---- ...grain_and_granularity__plan0_optimized.sql | 111 ++++---- ...ic_with_offset_window__plan0_optimized.sql | 109 ++++---- ...indow_and_granularity__plan0_optimized.sql | 109 ++++---- ...w_and_offset_to_grain__plan0_optimized.sql | 119 ++++---- ...grain_and_granularity__plan0_optimized.sql | 121 ++++---- ...indow_and_time_filter__plan0_optimized.sql | 117 ++++---- ...set_cumulative_metric__plan0_optimized.sql | 28 +- ...ric_with_agg_time_dim__plan0_optimized.sql | 89 +++--- ...with_one_input_metric__plan0_optimized.sql | 28 +- ...ulti_metric_fill_null__plan0_optimized.sql | 67 +++-- ...nested_derived_metric__plan0_optimized.sql | 132 +++++---- ...nstraint_not_selected__plan0_optimized.sql | 118 ++++---- ...ultiple_input_metrics__plan0_optimized.sql | 140 ++++++---- ...ls_without_time_spine__plan0_optimized.sql | 72 +++-- ...me_spine_multi_metric__plan0_optimized.sql | 87 +++--- .../test_nested_filters__plan0_optimized.sql | 133 +++++---- .../test_nested_offsets__plan0_optimized.sql | 91 +++--- ..._with_time_constraint__plan0_optimized.sql | 99 ++++--- ...with_where_constraint__plan0_optimized.sql | 105 ++++--- ...fferent_granularities__plan0_optimized.sql | 28 +- ...ultiple_granularities__plan0_optimized.sql | 36 ++- ...ain_with_agg_time_dim__plan0_optimized.sql | 109 ++++---- ...fferent_granularities__plan0_optimized.sql | 121 ++++---- ...ultiple_granularities__plan0_optimized.sql | 127 +++++---- ...dow_with_agg_time_dim__plan0_optimized.sql | 109 ++++---- ..._with_time_constraint__plan0_optimized.sql | 28 +- ..._with_time_constraint__plan0_optimized.sql | 28 +- .../test_derived_metric__plan0_optimized.sql | 29 +- ..._different_components__plan0_optimized.sql | 97 ++++--- ...ion_and_offset_window__plan0_optimized.sql | 28 +- ..._with_offset_to_grain__plan0_optimized.sql | 111 ++++---- ...grain_and_granularity__plan0_optimized.sql | 113 ++++---- ...ic_with_offset_window__plan0_optimized.sql | 111 ++++---- ...indow_and_granularity__plan0_optimized.sql | 111 ++++---- ...w_and_offset_to_grain__plan0_optimized.sql | 121 ++++---- ...grain_and_granularity__plan0_optimized.sql | 123 ++++---- ...indow_and_time_filter__plan0_optimized.sql | 119 ++++---- ...set_cumulative_metric__plan0_optimized.sql | 28 +- ...ric_with_agg_time_dim__plan0_optimized.sql | 91 +++--- ...with_one_input_metric__plan0_optimized.sql | 28 +- ...ulti_metric_fill_null__plan0_optimized.sql | 69 +++-- ...nested_derived_metric__plan0_optimized.sql | 134 +++++---- ...nstraint_not_selected__plan0_optimized.sql | 118 ++++---- ...ultiple_input_metrics__plan0_optimized.sql | 140 ++++++---- ...ls_without_time_spine__plan0_optimized.sql | 72 +++-- ...me_spine_multi_metric__plan0_optimized.sql | 89 +++--- .../test_nested_filters__plan0_optimized.sql | 133 +++++---- .../test_nested_offsets__plan0_optimized.sql | 91 +++--- ..._with_time_constraint__plan0_optimized.sql | 99 ++++--- ...with_where_constraint__plan0_optimized.sql | 105 ++++--- ...fferent_granularities__plan0_optimized.sql | 28 +- ...ultiple_granularities__plan0_optimized.sql | 36 ++- ...ain_with_agg_time_dim__plan0_optimized.sql | 111 ++++---- ...fferent_granularities__plan0_optimized.sql | 123 ++++---- ...ultiple_granularities__plan0_optimized.sql | 133 +++++---- ...dow_with_agg_time_dim__plan0_optimized.sql | 111 ++++---- ..._with_time_constraint__plan0_optimized.sql | 28 +- ..._with_time_constraint__plan0_optimized.sql | 28 +- .../test_derived_metric__plan0_optimized.sql | 29 +- ..._different_components__plan0_optimized.sql | 97 ++++--- ...ion_and_offset_window__plan0_optimized.sql | 28 +- ..._with_offset_to_grain__plan0_optimized.sql | 111 ++++---- ...grain_and_granularity__plan0_optimized.sql | 113 ++++---- ...ic_with_offset_window__plan0_optimized.sql | 111 ++++---- ...indow_and_granularity__plan0_optimized.sql | 111 ++++---- ...w_and_offset_to_grain__plan0_optimized.sql | 121 ++++---- ...grain_and_granularity__plan0_optimized.sql | 123 ++++---- ...indow_and_time_filter__plan0_optimized.sql | 119 ++++---- ...set_cumulative_metric__plan0_optimized.sql | 28 +- ...ric_with_agg_time_dim__plan0_optimized.sql | 91 +++--- ...with_one_input_metric__plan0_optimized.sql | 28 +- ...ulti_metric_fill_null__plan0_optimized.sql | 69 +++-- ...nested_derived_metric__plan0_optimized.sql | 134 +++++---- ...nstraint_not_selected__plan0_optimized.sql | 118 ++++---- ...ultiple_input_metrics__plan0_optimized.sql | 140 ++++++---- ...ls_without_time_spine__plan0_optimized.sql | 72 +++-- ...me_spine_multi_metric__plan0_optimized.sql | 89 +++--- .../test_nested_filters__plan0_optimized.sql | 133 +++++---- .../test_nested_offsets__plan0_optimized.sql | 91 +++--- ..._with_time_constraint__plan0_optimized.sql | 99 ++++--- ...with_where_constraint__plan0_optimized.sql | 105 ++++--- ...fferent_granularities__plan0_optimized.sql | 28 +- ...ultiple_granularities__plan0_optimized.sql | 36 ++- ...ain_with_agg_time_dim__plan0_optimized.sql | 111 ++++---- ...fferent_granularities__plan0_optimized.sql | 123 ++++---- ...ultiple_granularities__plan0_optimized.sql | 133 +++++---- ...dow_with_agg_time_dim__plan0_optimized.sql | 111 ++++---- ..._with_time_constraint__plan0_optimized.sql | 28 +- ..._with_time_constraint__plan0_optimized.sql | 28 +- .../test_derived_metric__plan0_optimized.sql | 29 +- ..._different_components__plan0_optimized.sql | 97 ++++--- ...ion_and_offset_window__plan0_optimized.sql | 28 +- ..._with_offset_to_grain__plan0_optimized.sql | 111 ++++---- ...grain_and_granularity__plan0_optimized.sql | 113 ++++---- ...ic_with_offset_window__plan0_optimized.sql | 111 ++++---- ...indow_and_granularity__plan0_optimized.sql | 111 ++++---- ...w_and_offset_to_grain__plan0_optimized.sql | 121 ++++---- ...grain_and_granularity__plan0_optimized.sql | 123 ++++---- ...indow_and_time_filter__plan0_optimized.sql | 119 ++++---- ...set_cumulative_metric__plan0_optimized.sql | 28 +- ...ric_with_agg_time_dim__plan0_optimized.sql | 91 +++--- ...with_one_input_metric__plan0_optimized.sql | 28 +- ...ulti_metric_fill_null__plan0_optimized.sql | 69 +++-- ...nested_derived_metric__plan0_optimized.sql | 134 +++++---- ...nstraint_not_selected__plan0_optimized.sql | 118 ++++---- ...ultiple_input_metrics__plan0_optimized.sql | 140 ++++++---- ...ls_without_time_spine__plan0_optimized.sql | 72 +++-- ...me_spine_multi_metric__plan0_optimized.sql | 89 +++--- .../test_nested_filters__plan0_optimized.sql | 133 +++++---- .../test_nested_offsets__plan0_optimized.sql | 91 +++--- ..._with_time_constraint__plan0_optimized.sql | 99 ++++--- ...with_where_constraint__plan0_optimized.sql | 105 ++++--- ...fferent_granularities__plan0_optimized.sql | 28 +- ...ultiple_granularities__plan0_optimized.sql | 36 ++- ...ain_with_agg_time_dim__plan0_optimized.sql | 111 ++++---- ...fferent_granularities__plan0_optimized.sql | 123 ++++---- ...ultiple_granularities__plan0_optimized.sql | 133 +++++---- ...dow_with_agg_time_dim__plan0_optimized.sql | 111 ++++---- ..._with_time_constraint__plan0_optimized.sql | 28 +- ..._with_time_constraint__plan0_optimized.sql | 28 +- .../test_derived_metric__plan0_optimized.sql | 29 +- ..._different_components__plan0_optimized.sql | 97 ++++--- ...ion_and_offset_window__plan0_optimized.sql | 28 +- ..._with_offset_to_grain__plan0_optimized.sql | 111 ++++---- ...grain_and_granularity__plan0_optimized.sql | 113 ++++---- ...ic_with_offset_window__plan0_optimized.sql | 111 ++++---- ...indow_and_granularity__plan0_optimized.sql | 111 ++++---- ...w_and_offset_to_grain__plan0_optimized.sql | 121 ++++---- ...grain_and_granularity__plan0_optimized.sql | 123 ++++---- ...indow_and_time_filter__plan0_optimized.sql | 119 ++++---- ...set_cumulative_metric__plan0_optimized.sql | 28 +- ...ric_with_agg_time_dim__plan0_optimized.sql | 91 +++--- ...with_one_input_metric__plan0_optimized.sql | 28 +- ...ulti_metric_fill_null__plan0_optimized.sql | 69 +++-- ...nested_derived_metric__plan0_optimized.sql | 134 +++++---- ...nstraint_not_selected__plan0_optimized.sql | 118 ++++---- ...ultiple_input_metrics__plan0_optimized.sql | 140 ++++++---- ...ls_without_time_spine__plan0_optimized.sql | 72 +++-- ...me_spine_multi_metric__plan0_optimized.sql | 89 +++--- .../test_nested_filters__plan0_optimized.sql | 133 +++++---- .../test_nested_offsets__plan0_optimized.sql | 91 +++--- ..._with_time_constraint__plan0_optimized.sql | 99 ++++--- ...with_where_constraint__plan0_optimized.sql | 105 ++++--- ...fferent_granularities__plan0_optimized.sql | 28 +- ...ultiple_granularities__plan0_optimized.sql | 36 ++- ...ain_with_agg_time_dim__plan0_optimized.sql | 111 ++++---- ...fferent_granularities__plan0_optimized.sql | 123 ++++---- ...ultiple_granularities__plan0_optimized.sql | 133 +++++---- ...dow_with_agg_time_dim__plan0_optimized.sql | 111 ++++---- ..._with_time_constraint__plan0_optimized.sql | 28 +- ..._with_time_constraint__plan0_optimized.sql | 28 +- .../test_derived_metric__plan0_optimized.sql | 29 +- ..._different_components__plan0_optimized.sql | 97 ++++--- ...ion_and_offset_window__plan0_optimized.sql | 28 +- ..._with_offset_to_grain__plan0_optimized.sql | 111 ++++---- ...grain_and_granularity__plan0_optimized.sql | 113 ++++---- ...ic_with_offset_window__plan0_optimized.sql | 111 ++++---- ...indow_and_granularity__plan0_optimized.sql | 111 ++++---- ...w_and_offset_to_grain__plan0_optimized.sql | 121 ++++---- ...grain_and_granularity__plan0_optimized.sql | 123 ++++---- ...indow_and_time_filter__plan0_optimized.sql | 119 ++++---- ...set_cumulative_metric__plan0_optimized.sql | 28 +- ...ric_with_agg_time_dim__plan0_optimized.sql | 91 +++--- ...with_one_input_metric__plan0_optimized.sql | 28 +- ...ulti_metric_fill_null__plan0_optimized.sql | 69 +++-- ...nested_derived_metric__plan0_optimized.sql | 134 +++++---- ...nstraint_not_selected__plan0_optimized.sql | 118 ++++---- ...ultiple_input_metrics__plan0_optimized.sql | 140 ++++++---- ...ls_without_time_spine__plan0_optimized.sql | 72 +++-- ...me_spine_multi_metric__plan0_optimized.sql | 89 +++--- .../test_nested_filters__plan0_optimized.sql | 133 +++++---- .../test_nested_offsets__plan0_optimized.sql | 91 +++--- ..._with_time_constraint__plan0_optimized.sql | 99 ++++--- ...with_where_constraint__plan0_optimized.sql | 105 ++++--- ...fferent_granularities__plan0_optimized.sql | 28 +- ...ultiple_granularities__plan0_optimized.sql | 36 ++- ...ain_with_agg_time_dim__plan0_optimized.sql | 111 ++++---- ...fferent_granularities__plan0_optimized.sql | 123 ++++---- ...ultiple_granularities__plan0_optimized.sql | 133 +++++---- ...dow_with_agg_time_dim__plan0_optimized.sql | 111 ++++---- ..._with_time_constraint__plan0_optimized.sql | 28 +- ..._for_one_input_metric__plan0_optimized.sql | 142 ++++++---- ..._for_one_input_metric__plan0_optimized.sql | 144 ++++++---- ..._for_one_input_metric__plan0_optimized.sql | 144 ++++++---- ..._for_one_input_metric__plan0_optimized.sql | 144 ++++++---- ..._for_one_input_metric__plan0_optimized.sql | 144 ++++++---- ..._for_one_input_metric__plan0_optimized.sql | 144 ++++++---- ...window_with_date_part__plan0_optimized.sql | 109 ++++---- ...ffset_to_grain_metric__plan0_optimized.sql | 28 +- ..._offset_window_metric__plan0_optimized.sql | 28 +- ...window_with_date_part__plan0_optimized.sql | 111 ++++---- ...ffset_to_grain_metric__plan0_optimized.sql | 28 +- ..._offset_window_metric__plan0_optimized.sql | 28 +- ...window_with_date_part__plan0_optimized.sql | 111 ++++---- ...ffset_to_grain_metric__plan0_optimized.sql | 28 +- ..._offset_window_metric__plan0_optimized.sql | 28 +- ...window_with_date_part__plan0_optimized.sql | 111 ++++---- ...ffset_to_grain_metric__plan0_optimized.sql | 28 +- ..._offset_window_metric__plan0_optimized.sql | 28 +- ...window_with_date_part__plan0_optimized.sql | 111 ++++---- ...ffset_to_grain_metric__plan0_optimized.sql | 28 +- ..._offset_window_metric__plan0_optimized.sql | 28 +- ...window_with_date_part__plan0_optimized.sql | 111 ++++---- ...ffset_to_grain_metric__plan0_optimized.sql | 28 +- ..._offset_window_metric__plan0_optimized.sql | 28 +- ...del_as_queried_metric__plan0_optimized.sql | 68 ++--- ...ith_conversion_metric__plan0_optimized.sql | 60 ++-- ...s_local_entity_prefix__plan0_optimized.sql | 75 ++--- ...inner_query_multi_hop__plan0_optimized.sql | 91 +++--- ...nner_query_single_hop__plan0_optimized.sql | 81 +++--- ...ic_filtered_by_itself__plan0_optimized.sql | 68 ++--- ...etric_in_where_filter__plan0_optimized.sql | 88 +++--- ...etric_in_where_filter__plan0_optimized.sql | 77 ++--- ...etric_in_where_filter__plan0_optimized.sql | 140 +++++----- ...ple_metrics_in_filter__plan0_optimized.sql | 109 ++++---- ...etric_in_where_filter__plan0_optimized.sql | 85 +++--- ...etric_in_where_filter__plan0_optimized.sql | 73 ++--- ...del_as_queried_metric__plan0_optimized.sql | 68 ++--- ...ith_conversion_metric__plan0_optimized.sql | 60 ++-- ...s_local_entity_prefix__plan0_optimized.sql | 75 ++--- ...inner_query_multi_hop__plan0_optimized.sql | 91 +++--- ...nner_query_single_hop__plan0_optimized.sql | 81 +++--- ...ic_filtered_by_itself__plan0_optimized.sql | 68 ++--- ...etric_in_where_filter__plan0_optimized.sql | 88 +++--- ...etric_in_where_filter__plan0_optimized.sql | 77 ++--- ...etric_in_where_filter__plan0_optimized.sql | 142 +++++----- ...ple_metrics_in_filter__plan0_optimized.sql | 109 ++++---- ...etric_in_where_filter__plan0_optimized.sql | 85 +++--- ...etric_in_where_filter__plan0_optimized.sql | 73 ++--- ...del_as_queried_metric__plan0_optimized.sql | 68 ++--- ...ith_conversion_metric__plan0_optimized.sql | 60 ++-- ...s_local_entity_prefix__plan0_optimized.sql | 75 ++--- ...inner_query_multi_hop__plan0_optimized.sql | 91 +++--- ...nner_query_single_hop__plan0_optimized.sql | 81 +++--- ...ic_filtered_by_itself__plan0_optimized.sql | 68 ++--- ...etric_in_where_filter__plan0_optimized.sql | 88 +++--- ...etric_in_where_filter__plan0_optimized.sql | 77 ++--- ...etric_in_where_filter__plan0_optimized.sql | 142 +++++----- ...ple_metrics_in_filter__plan0_optimized.sql | 109 ++++---- ...etric_in_where_filter__plan0_optimized.sql | 85 +++--- ...etric_in_where_filter__plan0_optimized.sql | 73 ++--- ...del_as_queried_metric__plan0_optimized.sql | 68 ++--- ...ith_conversion_metric__plan0_optimized.sql | 60 ++-- ...s_local_entity_prefix__plan0_optimized.sql | 75 ++--- ...inner_query_multi_hop__plan0_optimized.sql | 91 +++--- ...nner_query_single_hop__plan0_optimized.sql | 81 +++--- ...ic_filtered_by_itself__plan0_optimized.sql | 68 ++--- ...etric_in_where_filter__plan0_optimized.sql | 88 +++--- ...etric_in_where_filter__plan0_optimized.sql | 77 ++--- ...etric_in_where_filter__plan0_optimized.sql | 142 +++++----- ...ple_metrics_in_filter__plan0_optimized.sql | 109 ++++---- ...etric_in_where_filter__plan0_optimized.sql | 85 +++--- ...etric_in_where_filter__plan0_optimized.sql | 73 ++--- ...del_as_queried_metric__plan0_optimized.sql | 68 ++--- ...ith_conversion_metric__plan0_optimized.sql | 60 ++-- ...s_local_entity_prefix__plan0_optimized.sql | 75 ++--- ...inner_query_multi_hop__plan0_optimized.sql | 91 +++--- ...nner_query_single_hop__plan0_optimized.sql | 81 +++--- ...ic_filtered_by_itself__plan0_optimized.sql | 68 ++--- ...etric_in_where_filter__plan0_optimized.sql | 88 +++--- ...etric_in_where_filter__plan0_optimized.sql | 77 ++--- ...etric_in_where_filter__plan0_optimized.sql | 142 +++++----- ...ple_metrics_in_filter__plan0_optimized.sql | 109 ++++---- ...etric_in_where_filter__plan0_optimized.sql | 85 +++--- ...etric_in_where_filter__plan0_optimized.sql | 73 ++--- ...del_as_queried_metric__plan0_optimized.sql | 68 ++--- ...ith_conversion_metric__plan0_optimized.sql | 60 ++-- ...s_local_entity_prefix__plan0_optimized.sql | 75 ++--- ...inner_query_multi_hop__plan0_optimized.sql | 91 +++--- ...nner_query_single_hop__plan0_optimized.sql | 81 +++--- ...ic_filtered_by_itself__plan0_optimized.sql | 68 ++--- ...etric_in_where_filter__plan0_optimized.sql | 88 +++--- ...etric_in_where_filter__plan0_optimized.sql | 77 ++--- ...etric_in_where_filter__plan0_optimized.sql | 142 +++++----- ...ple_metrics_in_filter__plan0_optimized.sql | 109 ++++---- ...etric_in_where_filter__plan0_optimized.sql | 85 +++--- ...etric_in_where_filter__plan0_optimized.sql | 73 ++--- ...metric_time_dimension__plan0_optimized.sql | 23 +- ...metric_time_dimension__plan0_optimized.sql | 25 +- ...metric_time_dimension__plan0_optimized.sql | 25 +- ...metric_time_dimension__plan0_optimized.sql | 25 +- ...metric_time_dimension__plan0_optimized.sql | 25 +- ...metric_time_dimension__plan0_optimized.sql | 25 +- ...categorical_dimension__plan0_optimized.sql | 93 ++++--- ...ic_predicate_pushdown__plan0_optimized.sql | 263 ++++++++++-------- ...th_query_time_filters__plan0_optimized.sql | 188 +++++++------ ...tric_joins_and_filter__plan0_optimized.sql | 177 ++++++------ ...categorical_dimension__plan0_optimized.sql | 95 ++++--- ...ic_predicate_pushdown__plan0_optimized.sql | 263 ++++++++++-------- ...th_query_time_filters__plan0_optimized.sql | 192 +++++++------ ...tric_joins_and_filter__plan0_optimized.sql | 181 ++++++------ ...categorical_dimension__plan0_optimized.sql | 95 ++++--- ...ic_predicate_pushdown__plan0_optimized.sql | 263 ++++++++++-------- ...th_query_time_filters__plan0_optimized.sql | 192 +++++++------ ...tric_joins_and_filter__plan0_optimized.sql | 181 ++++++------ ...categorical_dimension__plan0_optimized.sql | 95 ++++--- ...ic_predicate_pushdown__plan0_optimized.sql | 263 ++++++++++-------- ...th_query_time_filters__plan0_optimized.sql | 192 +++++++------ ...tric_joins_and_filter__plan0_optimized.sql | 181 ++++++------ ...categorical_dimension__plan0_optimized.sql | 95 ++++--- ...ic_predicate_pushdown__plan0_optimized.sql | 263 ++++++++++-------- ...th_query_time_filters__plan0_optimized.sql | 192 +++++++------ ...tric_joins_and_filter__plan0_optimized.sql | 181 ++++++------ ...categorical_dimension__plan0_optimized.sql | 95 ++++--- ...ic_predicate_pushdown__plan0_optimized.sql | 263 ++++++++++-------- ...th_query_time_filters__plan0_optimized.sql | 192 +++++++------ ...tric_joins_and_filter__plan0_optimized.sql | 181 ++++++------ ...st_measure_constraint__plan0_optimized.sql | 125 +++++---- ...t_with_reused_measure__plan0_optimized.sql | 93 ++++--- ...single_expr_and_alias__plan0_optimized.sql | 28 +- ...sources_no_dimensions__plan0_optimized.sql | 28 +- ...metrics_no_dimensions__plan0_optimized.sql | 19 +- ...st_measure_constraint__plan0_optimized.sql | 127 +++++---- ...t_with_reused_measure__plan0_optimized.sql | 95 ++++--- ...single_expr_and_alias__plan0_optimized.sql | 28 +- ...sources_no_dimensions__plan0_optimized.sql | 28 +- ...metrics_no_dimensions__plan0_optimized.sql | 19 +- ...st_measure_constraint__plan0_optimized.sql | 127 +++++---- ...t_with_reused_measure__plan0_optimized.sql | 95 ++++--- ...single_expr_and_alias__plan0_optimized.sql | 28 +- ...sources_no_dimensions__plan0_optimized.sql | 28 +- ...metrics_no_dimensions__plan0_optimized.sql | 19 +- ...st_measure_constraint__plan0_optimized.sql | 127 +++++---- ...t_with_reused_measure__plan0_optimized.sql | 95 ++++--- ...single_expr_and_alias__plan0_optimized.sql | 28 +- ...sources_no_dimensions__plan0_optimized.sql | 28 +- ...metrics_no_dimensions__plan0_optimized.sql | 19 +- ...st_measure_constraint__plan0_optimized.sql | 127 +++++---- ...t_with_reused_measure__plan0_optimized.sql | 95 ++++--- ...single_expr_and_alias__plan0_optimized.sql | 28 +- ...sources_no_dimensions__plan0_optimized.sql | 28 +- ...metrics_no_dimensions__plan0_optimized.sql | 19 +- ...st_measure_constraint__plan0_optimized.sql | 127 +++++---- ...t_with_reused_measure__plan0_optimized.sql | 95 ++++--- ...single_expr_and_alias__plan0_optimized.sql | 28 +- ...sources_no_dimensions__plan0_optimized.sql | 28 +- ...metrics_no_dimensions__plan0_optimized.sql | 19 +- .../BigQuery/test_id_enumeration__query.sql | 23 +- .../Databricks/test_id_enumeration__query.sql | 25 +- .../Postgres/test_id_enumeration__query.sql | 25 +- .../Redshift/test_id_enumeration__query.sql | 25 +- .../Snowflake/test_id_enumeration__query.sql | 25 +- .../str/Trino/test_id_enumeration__query.sql | 25 +- 396 files changed, 19886 insertions(+), 14216 deletions(-) diff --git a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql index 1548db468..06c7e2c0d 100644 --- a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql @@ -4,44 +4,60 @@ docstring: Test querying a derived metric with a cumulative input metric using non-default grains. sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , t2mr - 10 AS trailing_2_months_revenue_sub_10 -FROM ( - -- Re-aggregate Metric via Group By +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , txn_revenue AS t2mr + FROM ( + -- Join Self Over Time Range + -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] + -- Aggregate Measures + SELECT + subq_13.ds AS metric_time__day + , DATETIME_TRUNC(subq_13.ds, isoweek) AS metric_time__week + , SUM(revenue_src_28000.revenue) AS txn_revenue + FROM ***************************.mf_time_spine subq_13 + INNER JOIN + ***************************.fct_revenue revenue_src_28000 + ON + ( + DATETIME_TRUNC(revenue_src_28000.created_at, day) <= subq_13.ds + ) AND ( + DATETIME_TRUNC(revenue_src_28000.created_at, day) > DATE_SUB(CAST(subq_13.ds AS DATETIME), INTERVAL 2 month) + ) + GROUP BY + metric_time__day + , metric_time__week + ) subq_16 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__week - , t2mr + , t2mr - 10 AS trailing_2_months_revenue_sub_10 FROM ( - -- Compute Metrics via Expressions - -- Window Function for Metric Re-aggregation + -- Re-aggregate Metric via Group By SELECT metric_time__week - , AVG(txn_revenue) OVER (PARTITION BY metric_time__week) AS t2mr + , t2mr FROM ( - -- Join Self Over Time Range - -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] - -- Aggregate Measures + -- Read From CTE For node_id=cm_4 + -- Window Function for Metric Re-aggregation SELECT - subq_13.ds AS metric_time__day - , DATETIME_TRUNC(subq_13.ds, isoweek) AS metric_time__week - , SUM(revenue_src_28000.revenue) AS txn_revenue - FROM ***************************.mf_time_spine subq_13 - INNER JOIN - ***************************.fct_revenue revenue_src_28000 - ON - ( - DATETIME_TRUNC(revenue_src_28000.created_at, day) <= subq_13.ds - ) AND ( - DATETIME_TRUNC(revenue_src_28000.created_at, day) > DATE_SUB(CAST(subq_13.ds AS DATETIME), INTERVAL 2 month) - ) - GROUP BY - metric_time__day - , metric_time__week - ) subq_16 - ) subq_18 - GROUP BY - metric_time__week - , t2mr -) subq_19 + metric_time__week + , AVG(t2mr) OVER (PARTITION BY metric_time__week) AS t2mr + FROM cm_4_cte cm_4_cte + ) subq_18 + GROUP BY + metric_time__week + , t2mr + ) subq_19 +) + +SELECT + metric_time__week AS metric_time__week + , trailing_2_months_revenue_sub_10 AS trailing_2_months_revenue_sub_10 +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql index 2c2a13232..db41ff9b9 100644 --- a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql @@ -4,44 +4,60 @@ docstring: Test querying a derived metric with a cumulative input metric using non-default grains. sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , t2mr - 10 AS trailing_2_months_revenue_sub_10 -FROM ( - -- Re-aggregate Metric via Group By +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , txn_revenue AS t2mr + FROM ( + -- Join Self Over Time Range + -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] + -- Aggregate Measures + SELECT + subq_13.ds AS metric_time__day + , DATE_TRUNC('week', subq_13.ds) AS metric_time__week + , SUM(revenue_src_28000.revenue) AS txn_revenue + FROM ***************************.mf_time_spine subq_13 + INNER JOIN + ***************************.fct_revenue revenue_src_28000 + ON + ( + DATE_TRUNC('day', revenue_src_28000.created_at) <= subq_13.ds + ) AND ( + DATE_TRUNC('day', revenue_src_28000.created_at) > DATEADD(month, -2, subq_13.ds) + ) + GROUP BY + subq_13.ds + , DATE_TRUNC('week', subq_13.ds) + ) subq_16 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__week - , t2mr + , t2mr - 10 AS trailing_2_months_revenue_sub_10 FROM ( - -- Compute Metrics via Expressions - -- Window Function for Metric Re-aggregation + -- Re-aggregate Metric via Group By SELECT metric_time__week - , AVG(txn_revenue) OVER (PARTITION BY metric_time__week) AS t2mr + , t2mr FROM ( - -- Join Self Over Time Range - -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] - -- Aggregate Measures + -- Read From CTE For node_id=cm_4 + -- Window Function for Metric Re-aggregation SELECT - subq_13.ds AS metric_time__day - , DATE_TRUNC('week', subq_13.ds) AS metric_time__week - , SUM(revenue_src_28000.revenue) AS txn_revenue - FROM ***************************.mf_time_spine subq_13 - INNER JOIN - ***************************.fct_revenue revenue_src_28000 - ON - ( - DATE_TRUNC('day', revenue_src_28000.created_at) <= subq_13.ds - ) AND ( - DATE_TRUNC('day', revenue_src_28000.created_at) > DATEADD(month, -2, subq_13.ds) - ) - GROUP BY - subq_13.ds - , DATE_TRUNC('week', subq_13.ds) - ) subq_16 - ) subq_18 - GROUP BY - metric_time__week - , t2mr -) subq_19 + metric_time__week + , AVG(t2mr) OVER (PARTITION BY metric_time__week) AS t2mr + FROM cm_4_cte cm_4_cte + ) subq_18 + GROUP BY + metric_time__week + , t2mr + ) subq_19 +) + +SELECT + metric_time__week AS metric_time__week + , trailing_2_months_revenue_sub_10 AS trailing_2_months_revenue_sub_10 +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql index d43053690..2d16aaefe 100644 --- a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql @@ -4,44 +4,60 @@ docstring: Test querying a derived metric with a cumulative input metric using non-default grains. sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , t2mr - 10 AS trailing_2_months_revenue_sub_10 -FROM ( - -- Re-aggregate Metric via Group By +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , txn_revenue AS t2mr + FROM ( + -- Join Self Over Time Range + -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] + -- Aggregate Measures + SELECT + subq_13.ds AS metric_time__day + , DATE_TRUNC('week', subq_13.ds) AS metric_time__week + , SUM(revenue_src_28000.revenue) AS txn_revenue + FROM ***************************.mf_time_spine subq_13 + INNER JOIN + ***************************.fct_revenue revenue_src_28000 + ON + ( + DATE_TRUNC('day', revenue_src_28000.created_at) <= subq_13.ds + ) AND ( + DATE_TRUNC('day', revenue_src_28000.created_at) > subq_13.ds - MAKE_INTERVAL(months => 2) + ) + GROUP BY + subq_13.ds + , DATE_TRUNC('week', subq_13.ds) + ) subq_16 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__week - , t2mr + , t2mr - 10 AS trailing_2_months_revenue_sub_10 FROM ( - -- Compute Metrics via Expressions - -- Window Function for Metric Re-aggregation + -- Re-aggregate Metric via Group By SELECT metric_time__week - , AVG(txn_revenue) OVER (PARTITION BY metric_time__week) AS t2mr + , t2mr FROM ( - -- Join Self Over Time Range - -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] - -- Aggregate Measures + -- Read From CTE For node_id=cm_4 + -- Window Function for Metric Re-aggregation SELECT - subq_13.ds AS metric_time__day - , DATE_TRUNC('week', subq_13.ds) AS metric_time__week - , SUM(revenue_src_28000.revenue) AS txn_revenue - FROM ***************************.mf_time_spine subq_13 - INNER JOIN - ***************************.fct_revenue revenue_src_28000 - ON - ( - DATE_TRUNC('day', revenue_src_28000.created_at) <= subq_13.ds - ) AND ( - DATE_TRUNC('day', revenue_src_28000.created_at) > subq_13.ds - MAKE_INTERVAL(months => 2) - ) - GROUP BY - subq_13.ds - , DATE_TRUNC('week', subq_13.ds) - ) subq_16 - ) subq_18 - GROUP BY - metric_time__week - , t2mr -) subq_19 + metric_time__week + , AVG(t2mr) OVER (PARTITION BY metric_time__week) AS t2mr + FROM cm_4_cte cm_4_cte + ) subq_18 + GROUP BY + metric_time__week + , t2mr + ) subq_19 +) + +SELECT + metric_time__week AS metric_time__week + , trailing_2_months_revenue_sub_10 AS trailing_2_months_revenue_sub_10 +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql index d0ce2c8ec..7470b5372 100644 --- a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql @@ -4,44 +4,60 @@ docstring: Test querying a derived metric with a cumulative input metric using non-default grains. sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , t2mr - 10 AS trailing_2_months_revenue_sub_10 -FROM ( - -- Re-aggregate Metric via Group By +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , txn_revenue AS t2mr + FROM ( + -- Join Self Over Time Range + -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] + -- Aggregate Measures + SELECT + subq_13.ds AS metric_time__day + , DATE_TRUNC('week', subq_13.ds) AS metric_time__week + , SUM(revenue_src_28000.revenue) AS txn_revenue + FROM ***************************.mf_time_spine subq_13 + INNER JOIN + ***************************.fct_revenue revenue_src_28000 + ON + ( + DATE_TRUNC('day', revenue_src_28000.created_at) <= subq_13.ds + ) AND ( + DATE_TRUNC('day', revenue_src_28000.created_at) > DATEADD(month, -2, subq_13.ds) + ) + GROUP BY + subq_13.ds + , DATE_TRUNC('week', subq_13.ds) + ) subq_16 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__week - , t2mr + , t2mr - 10 AS trailing_2_months_revenue_sub_10 FROM ( - -- Compute Metrics via Expressions - -- Window Function for Metric Re-aggregation + -- Re-aggregate Metric via Group By SELECT metric_time__week - , AVG(txn_revenue) OVER (PARTITION BY metric_time__week) AS t2mr + , t2mr FROM ( - -- Join Self Over Time Range - -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] - -- Aggregate Measures + -- Read From CTE For node_id=cm_4 + -- Window Function for Metric Re-aggregation SELECT - subq_13.ds AS metric_time__day - , DATE_TRUNC('week', subq_13.ds) AS metric_time__week - , SUM(revenue_src_28000.revenue) AS txn_revenue - FROM ***************************.mf_time_spine subq_13 - INNER JOIN - ***************************.fct_revenue revenue_src_28000 - ON - ( - DATE_TRUNC('day', revenue_src_28000.created_at) <= subq_13.ds - ) AND ( - DATE_TRUNC('day', revenue_src_28000.created_at) > DATEADD(month, -2, subq_13.ds) - ) - GROUP BY - subq_13.ds - , DATE_TRUNC('week', subq_13.ds) - ) subq_16 - ) subq_18 - GROUP BY - metric_time__week - , t2mr -) subq_19 + metric_time__week + , AVG(t2mr) OVER (PARTITION BY metric_time__week) AS t2mr + FROM cm_4_cte cm_4_cte + ) subq_18 + GROUP BY + metric_time__week + , t2mr + ) subq_19 +) + +SELECT + metric_time__week AS metric_time__week + , trailing_2_months_revenue_sub_10 AS trailing_2_months_revenue_sub_10 +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql index b0eac1b40..d16b558aa 100644 --- a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql @@ -4,44 +4,60 @@ docstring: Test querying a derived metric with a cumulative input metric using non-default grains. sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , t2mr - 10 AS trailing_2_months_revenue_sub_10 -FROM ( - -- Re-aggregate Metric via Group By +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , txn_revenue AS t2mr + FROM ( + -- Join Self Over Time Range + -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] + -- Aggregate Measures + SELECT + subq_13.ds AS metric_time__day + , DATE_TRUNC('week', subq_13.ds) AS metric_time__week + , SUM(revenue_src_28000.revenue) AS txn_revenue + FROM ***************************.mf_time_spine subq_13 + INNER JOIN + ***************************.fct_revenue revenue_src_28000 + ON + ( + DATE_TRUNC('day', revenue_src_28000.created_at) <= subq_13.ds + ) AND ( + DATE_TRUNC('day', revenue_src_28000.created_at) > DATEADD(month, -2, subq_13.ds) + ) + GROUP BY + subq_13.ds + , DATE_TRUNC('week', subq_13.ds) + ) subq_16 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__week - , t2mr + , t2mr - 10 AS trailing_2_months_revenue_sub_10 FROM ( - -- Compute Metrics via Expressions - -- Window Function for Metric Re-aggregation + -- Re-aggregate Metric via Group By SELECT metric_time__week - , AVG(txn_revenue) OVER (PARTITION BY metric_time__week) AS t2mr + , t2mr FROM ( - -- Join Self Over Time Range - -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] - -- Aggregate Measures + -- Read From CTE For node_id=cm_4 + -- Window Function for Metric Re-aggregation SELECT - subq_13.ds AS metric_time__day - , DATE_TRUNC('week', subq_13.ds) AS metric_time__week - , SUM(revenue_src_28000.revenue) AS txn_revenue - FROM ***************************.mf_time_spine subq_13 - INNER JOIN - ***************************.fct_revenue revenue_src_28000 - ON - ( - DATE_TRUNC('day', revenue_src_28000.created_at) <= subq_13.ds - ) AND ( - DATE_TRUNC('day', revenue_src_28000.created_at) > DATEADD(month, -2, subq_13.ds) - ) - GROUP BY - subq_13.ds - , DATE_TRUNC('week', subq_13.ds) - ) subq_16 - ) subq_18 - GROUP BY - metric_time__week - , t2mr -) subq_19 + metric_time__week + , AVG(t2mr) OVER (PARTITION BY metric_time__week) AS t2mr + FROM cm_4_cte cm_4_cte + ) subq_18 + GROUP BY + metric_time__week + , t2mr + ) subq_19 +) + +SELECT + metric_time__week AS metric_time__week + , trailing_2_months_revenue_sub_10 AS trailing_2_months_revenue_sub_10 +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Trino/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Trino/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql index f5070e701..c783f912b 100644 --- a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Trino/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Trino/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql @@ -4,44 +4,60 @@ docstring: Test querying a derived metric with a cumulative input metric using non-default grains. sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , t2mr - 10 AS trailing_2_months_revenue_sub_10 -FROM ( - -- Re-aggregate Metric via Group By +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , txn_revenue AS t2mr + FROM ( + -- Join Self Over Time Range + -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] + -- Aggregate Measures + SELECT + subq_13.ds AS metric_time__day + , DATE_TRUNC('week', subq_13.ds) AS metric_time__week + , SUM(revenue_src_28000.revenue) AS txn_revenue + FROM ***************************.mf_time_spine subq_13 + INNER JOIN + ***************************.fct_revenue revenue_src_28000 + ON + ( + DATE_TRUNC('day', revenue_src_28000.created_at) <= subq_13.ds + ) AND ( + DATE_TRUNC('day', revenue_src_28000.created_at) > DATE_ADD('month', -2, subq_13.ds) + ) + GROUP BY + subq_13.ds + , DATE_TRUNC('week', subq_13.ds) + ) subq_16 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__week - , t2mr + , t2mr - 10 AS trailing_2_months_revenue_sub_10 FROM ( - -- Compute Metrics via Expressions - -- Window Function for Metric Re-aggregation + -- Re-aggregate Metric via Group By SELECT metric_time__week - , AVG(txn_revenue) OVER (PARTITION BY metric_time__week) AS t2mr + , t2mr FROM ( - -- Join Self Over Time Range - -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] - -- Aggregate Measures + -- Read From CTE For node_id=cm_4 + -- Window Function for Metric Re-aggregation SELECT - subq_13.ds AS metric_time__day - , DATE_TRUNC('week', subq_13.ds) AS metric_time__week - , SUM(revenue_src_28000.revenue) AS txn_revenue - FROM ***************************.mf_time_spine subq_13 - INNER JOIN - ***************************.fct_revenue revenue_src_28000 - ON - ( - DATE_TRUNC('day', revenue_src_28000.created_at) <= subq_13.ds - ) AND ( - DATE_TRUNC('day', revenue_src_28000.created_at) > DATE_ADD('month', -2, subq_13.ds) - ) - GROUP BY - subq_13.ds - , DATE_TRUNC('week', subq_13.ds) - ) subq_16 - ) subq_18 - GROUP BY - metric_time__week - , t2mr -) subq_19 + metric_time__week + , AVG(t2mr) OVER (PARTITION BY metric_time__week) AS t2mr + FROM cm_4_cte cm_4_cte + ) subq_18 + GROUP BY + metric_time__week + , t2mr + ) subq_19 +) + +SELECT + metric_time__week AS metric_time__week + , trailing_2_months_revenue_sub_10 AS trailing_2_months_revenue_sub_10 +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_derived_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_derived_metric_with_custom_granularity__plan0_optimized.sql index 5608d1dfa..9fcbb5bd7 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_derived_metric_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_derived_metric_with_custom_granularity__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , booking_value * 0.05 / bookers AS booking_fees_per_booker -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['booking_value', 'bookers', 'booking__ds__martian_day'] @@ -23,4 +20,24 @@ FROM ( DATETIME_TRUNC(bookings_source_src_28000.ds, day) = subq_14.ds GROUP BY booking__ds__martian_day -) subq_18 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , booking_value * 0.05 / bookers AS booking_fees_per_booker + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + booking__ds__martian_day + , booking_value + , bookers + FROM cm_8_cte cm_8_cte + ) subq_18 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , booking_fees_per_booker AS booking_fees_per_booker +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql index ec57d54bf..9186c3693 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql @@ -3,11 +3,7 @@ test_filename: test_custom_granularity.py sql_engine: BigQuery --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) AS metric_time__martian_day - , MAX(subq_17.bookings) AS bookings - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_4_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'metric_time__martian_day'] @@ -29,8 +25,9 @@ FROM ( subq_12.ds__day = subq_13.ds GROUP BY metric_time__martian_day -) subq_17 -FULL OUTER JOIN ( +) + +, cm_5_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['listings', 'metric_time__martian_day'] @@ -52,8 +49,16 @@ FULL OUTER JOIN ( subq_18.ds__day = subq_19.ds GROUP BY metric_time__martian_day -) subq_23 +) + +SELECT + COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day) AS metric_time__martian_day + , MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +FULL OUTER JOIN + cm_5_cte cm_5_cte ON - subq_17.metric_time__martian_day = subq_23.metric_time__martian_day + cm_4_cte.metric_time__martian_day = cm_5_cte.metric_time__martian_day GROUP BY metric_time__martian_day diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_offset_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_offset_metric_with_custom_granularity__plan0_optimized.sql index ac6a6b216..2a8075f5f 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_offset_metric_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_offset_metric_with_custom_granularity__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'booking__ds__martian_day'] @@ -32,4 +29,23 @@ FROM ( subq_12.ds = subq_13.ds GROUP BY booking__ds__martian_day -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + booking__ds__martian_day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql index 4239dbf28..75c6792e9 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity_filter_not_in_group_by test_filename: test_custom_granularity.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -40,4 +37,23 @@ FROM ( WHERE metric_time__martian_day = '2020-01-01' GROUP BY metric_time__day -) subq_19 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_19 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_derived_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_derived_metric_with_custom_granularity__plan0_optimized.sql index 33b3c5091..5b6591908 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_derived_metric_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_derived_metric_with_custom_granularity__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , booking_value * 0.05 / bookers AS booking_fees_per_booker -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['booking_value', 'bookers', 'booking__ds__martian_day'] @@ -23,4 +20,24 @@ FROM ( DATE_TRUNC('day', bookings_source_src_28000.ds) = subq_14.ds GROUP BY subq_14.martian_day -) subq_18 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , booking_value * 0.05 / bookers AS booking_fees_per_booker + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + booking__ds__martian_day + , booking_value + , bookers + FROM cm_8_cte cm_8_cte + ) subq_18 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , booking_fees_per_booker AS booking_fees_per_booker +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql index 38f2bc748..55b66c93e 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql @@ -3,11 +3,7 @@ test_filename: test_custom_granularity.py sql_engine: Databricks --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) AS metric_time__martian_day - , MAX(subq_17.bookings) AS bookings - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_4_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'metric_time__martian_day'] @@ -29,8 +25,9 @@ FROM ( subq_12.ds__day = subq_13.ds GROUP BY subq_13.martian_day -) subq_17 -FULL OUTER JOIN ( +) + +, cm_5_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['listings', 'metric_time__martian_day'] @@ -52,8 +49,16 @@ FULL OUTER JOIN ( subq_18.ds__day = subq_19.ds GROUP BY subq_19.martian_day -) subq_23 +) + +SELECT + COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day) AS metric_time__martian_day + , MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +FULL OUTER JOIN + cm_5_cte cm_5_cte ON - subq_17.metric_time__martian_day = subq_23.metric_time__martian_day + cm_4_cte.metric_time__martian_day = cm_5_cte.metric_time__martian_day GROUP BY - COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) + COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day) diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_offset_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_offset_metric_with_custom_granularity__plan0_optimized.sql index cc3c8aeff..0f427c8e5 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_offset_metric_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_offset_metric_with_custom_granularity__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'booking__ds__martian_day'] @@ -32,4 +29,23 @@ FROM ( subq_12.ds = subq_13.ds GROUP BY subq_13.martian_day -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + booking__ds__martian_day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql index e4cb94adb..3ec93b245 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity_filter_not_in_group_by test_filename: test_custom_granularity.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -40,4 +37,23 @@ FROM ( WHERE metric_time__martian_day = '2020-01-01' GROUP BY metric_time__day -) subq_19 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_19 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_derived_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_derived_metric_with_custom_granularity__plan0_optimized.sql index 22a163a26..99a872492 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_derived_metric_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_derived_metric_with_custom_granularity__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , booking_value * 0.05 / bookers AS booking_fees_per_booker -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['booking_value', 'bookers', 'booking__ds__martian_day'] @@ -23,4 +20,24 @@ FROM ( DATE_TRUNC('day', bookings_source_src_28000.ds) = subq_14.ds GROUP BY subq_14.martian_day -) subq_18 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , booking_value * 0.05 / bookers AS booking_fees_per_booker + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + booking__ds__martian_day + , booking_value + , bookers + FROM cm_8_cte cm_8_cte + ) subq_18 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , booking_fees_per_booker AS booking_fees_per_booker +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql index 88f0d5e65..60e619523 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql @@ -3,11 +3,7 @@ test_filename: test_custom_granularity.py sql_engine: Postgres --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) AS metric_time__martian_day - , MAX(subq_17.bookings) AS bookings - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_4_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'metric_time__martian_day'] @@ -29,8 +25,9 @@ FROM ( subq_12.ds__day = subq_13.ds GROUP BY subq_13.martian_day -) subq_17 -FULL OUTER JOIN ( +) + +, cm_5_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['listings', 'metric_time__martian_day'] @@ -52,8 +49,16 @@ FULL OUTER JOIN ( subq_18.ds__day = subq_19.ds GROUP BY subq_19.martian_day -) subq_23 +) + +SELECT + COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day) AS metric_time__martian_day + , MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +FULL OUTER JOIN + cm_5_cte cm_5_cte ON - subq_17.metric_time__martian_day = subq_23.metric_time__martian_day + cm_4_cte.metric_time__martian_day = cm_5_cte.metric_time__martian_day GROUP BY - COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) + COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day) diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_offset_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_offset_metric_with_custom_granularity__plan0_optimized.sql index 1a03d0f71..b53cdf5ac 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_offset_metric_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_offset_metric_with_custom_granularity__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'booking__ds__martian_day'] @@ -32,4 +29,23 @@ FROM ( subq_12.ds = subq_13.ds GROUP BY subq_13.martian_day -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + booking__ds__martian_day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql index e5cb95f9b..212108b24 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity_filter_not_in_group_by test_filename: test_custom_granularity.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -40,4 +37,23 @@ FROM ( WHERE metric_time__martian_day = '2020-01-01' GROUP BY metric_time__day -) subq_19 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_19 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_derived_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_derived_metric_with_custom_granularity__plan0_optimized.sql index 0faeff60b..6410848b9 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_derived_metric_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_derived_metric_with_custom_granularity__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , booking_value * 0.05 / bookers AS booking_fees_per_booker -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['booking_value', 'bookers', 'booking__ds__martian_day'] @@ -23,4 +20,24 @@ FROM ( DATE_TRUNC('day', bookings_source_src_28000.ds) = subq_14.ds GROUP BY subq_14.martian_day -) subq_18 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , booking_value * 0.05 / bookers AS booking_fees_per_booker + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + booking__ds__martian_day + , booking_value + , bookers + FROM cm_8_cte cm_8_cte + ) subq_18 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , booking_fees_per_booker AS booking_fees_per_booker +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql index 1c8b925d2..5a4041fd6 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql @@ -3,11 +3,7 @@ test_filename: test_custom_granularity.py sql_engine: Redshift --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) AS metric_time__martian_day - , MAX(subq_17.bookings) AS bookings - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_4_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'metric_time__martian_day'] @@ -29,8 +25,9 @@ FROM ( subq_12.ds__day = subq_13.ds GROUP BY subq_13.martian_day -) subq_17 -FULL OUTER JOIN ( +) + +, cm_5_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['listings', 'metric_time__martian_day'] @@ -52,8 +49,16 @@ FULL OUTER JOIN ( subq_18.ds__day = subq_19.ds GROUP BY subq_19.martian_day -) subq_23 +) + +SELECT + COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day) AS metric_time__martian_day + , MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +FULL OUTER JOIN + cm_5_cte cm_5_cte ON - subq_17.metric_time__martian_day = subq_23.metric_time__martian_day + cm_4_cte.metric_time__martian_day = cm_5_cte.metric_time__martian_day GROUP BY - COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) + COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day) diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_offset_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_offset_metric_with_custom_granularity__plan0_optimized.sql index 70b91bc6a..2ee8a2791 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_offset_metric_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_offset_metric_with_custom_granularity__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'booking__ds__martian_day'] @@ -32,4 +29,23 @@ FROM ( subq_12.ds = subq_13.ds GROUP BY subq_13.martian_day -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + booking__ds__martian_day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql index 40e9bafc5..4f63900d9 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity_filter_not_in_group_by test_filename: test_custom_granularity.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -40,4 +37,23 @@ FROM ( WHERE metric_time__martian_day = '2020-01-01' GROUP BY metric_time__day -) subq_19 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_19 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_derived_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_derived_metric_with_custom_granularity__plan0_optimized.sql index dadf4178a..efa5cd4e3 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_derived_metric_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_derived_metric_with_custom_granularity__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , booking_value * 0.05 / bookers AS booking_fees_per_booker -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['booking_value', 'bookers', 'booking__ds__martian_day'] @@ -23,4 +20,24 @@ FROM ( DATE_TRUNC('day', bookings_source_src_28000.ds) = subq_14.ds GROUP BY subq_14.martian_day -) subq_18 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , booking_value * 0.05 / bookers AS booking_fees_per_booker + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + booking__ds__martian_day + , booking_value + , bookers + FROM cm_8_cte cm_8_cte + ) subq_18 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , booking_fees_per_booker AS booking_fees_per_booker +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql index d4164aaf0..8691ac005 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql @@ -3,11 +3,7 @@ test_filename: test_custom_granularity.py sql_engine: Snowflake --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) AS metric_time__martian_day - , MAX(subq_17.bookings) AS bookings - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_4_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'metric_time__martian_day'] @@ -29,8 +25,9 @@ FROM ( subq_12.ds__day = subq_13.ds GROUP BY subq_13.martian_day -) subq_17 -FULL OUTER JOIN ( +) + +, cm_5_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['listings', 'metric_time__martian_day'] @@ -52,8 +49,16 @@ FULL OUTER JOIN ( subq_18.ds__day = subq_19.ds GROUP BY subq_19.martian_day -) subq_23 +) + +SELECT + COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day) AS metric_time__martian_day + , MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +FULL OUTER JOIN + cm_5_cte cm_5_cte ON - subq_17.metric_time__martian_day = subq_23.metric_time__martian_day + cm_4_cte.metric_time__martian_day = cm_5_cte.metric_time__martian_day GROUP BY - COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) + COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day) diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_offset_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_offset_metric_with_custom_granularity__plan0_optimized.sql index 8d106e868..cb0db8ef7 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_offset_metric_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_offset_metric_with_custom_granularity__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'booking__ds__martian_day'] @@ -32,4 +29,23 @@ FROM ( subq_12.ds = subq_13.ds GROUP BY subq_13.martian_day -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + booking__ds__martian_day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql index e86c0510f..49755bcde 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity_filter_not_in_group_by test_filename: test_custom_granularity.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -40,4 +37,23 @@ FROM ( WHERE metric_time__martian_day = '2020-01-01' GROUP BY metric_time__day -) subq_19 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_19 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_derived_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_derived_metric_with_custom_granularity__plan0_optimized.sql index 0396ee400..22c615b5f 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_derived_metric_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_derived_metric_with_custom_granularity__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , booking_value * 0.05 / bookers AS booking_fees_per_booker -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['booking_value', 'bookers', 'booking__ds__martian_day'] @@ -23,4 +20,24 @@ FROM ( DATE_TRUNC('day', bookings_source_src_28000.ds) = subq_14.ds GROUP BY subq_14.martian_day -) subq_18 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , booking_value * 0.05 / bookers AS booking_fees_per_booker + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + booking__ds__martian_day + , booking_value + , bookers + FROM cm_8_cte cm_8_cte + ) subq_18 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , booking_fees_per_booker AS booking_fees_per_booker +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql index 8f00e938d..cfbbfc89b 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql @@ -3,11 +3,7 @@ test_filename: test_custom_granularity.py sql_engine: Trino --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) AS metric_time__martian_day - , MAX(subq_17.bookings) AS bookings - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_4_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'metric_time__martian_day'] @@ -29,8 +25,9 @@ FROM ( subq_12.ds__day = subq_13.ds GROUP BY subq_13.martian_day -) subq_17 -FULL OUTER JOIN ( +) + +, cm_5_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['listings', 'metric_time__martian_day'] @@ -52,8 +49,16 @@ FULL OUTER JOIN ( subq_18.ds__day = subq_19.ds GROUP BY subq_19.martian_day -) subq_23 +) + +SELECT + COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day) AS metric_time__martian_day + , MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +FULL OUTER JOIN + cm_5_cte cm_5_cte ON - subq_17.metric_time__martian_day = subq_23.metric_time__martian_day + cm_4_cte.metric_time__martian_day = cm_5_cte.metric_time__martian_day GROUP BY - COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) + COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day) diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_offset_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_offset_metric_with_custom_granularity__plan0_optimized.sql index 4ab73ec73..def146c83 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_offset_metric_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_offset_metric_with_custom_granularity__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'booking__ds__martian_day'] @@ -32,4 +29,23 @@ FROM ( subq_12.ds = subq_13.ds GROUP BY subq_13.martian_day -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + booking__ds__martian_day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql index 2ed4e1008..61708b526 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity_filter_not_in_group_by test_filename: test_custom_granularity.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -40,4 +37,23 @@ FROM ( WHERE metric_time__martian_day = '2020-01-01' GROUP BY metric_time__day -) subq_19 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_19 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_combined_metrics_plan__ep_0.xml index 75da5fd0b..d06c76969 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_combined_metrics_plan__ep_0.xml @@ -5,82 +5,90 @@ test_filename: test_dataflow_to_execution.py - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_small_combined_metrics_plan__ep_0.xml index 248cf24e4..b368cfbda 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_small_combined_metrics_plan__ep_0.xml @@ -17,11 +17,7 @@ test_filename: test_dataflow_to_execution.py - - - - - + @@ -32,8 +28,9 @@ test_filename: test_dataflow_to_execution.py - - + + + @@ -44,9 +41,17 @@ test_filename: test_dataflow_to_execution.py - + + + + + + + + + - + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_combined_metrics_plan__ep_0.xml index 2d133f4c6..24ab5848f 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_combined_metrics_plan__ep_0.xml @@ -5,82 +5,90 @@ test_filename: test_dataflow_to_execution.py - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_small_combined_metrics_plan__ep_0.xml index 4da40aaf0..1eff644ad 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_small_combined_metrics_plan__ep_0.xml @@ -17,11 +17,7 @@ test_filename: test_dataflow_to_execution.py - - - - - + @@ -32,8 +28,9 @@ test_filename: test_dataflow_to_execution.py - - + + + @@ -44,10 +41,18 @@ test_filename: test_dataflow_to_execution.py - + + + + + + + + + - + - + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_combined_metrics_plan__ep_0.xml index 2d133f4c6..24ab5848f 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_combined_metrics_plan__ep_0.xml @@ -5,82 +5,90 @@ test_filename: test_dataflow_to_execution.py - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_small_combined_metrics_plan__ep_0.xml index 4da40aaf0..1eff644ad 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_small_combined_metrics_plan__ep_0.xml @@ -17,11 +17,7 @@ test_filename: test_dataflow_to_execution.py - - - - - + @@ -32,8 +28,9 @@ test_filename: test_dataflow_to_execution.py - - + + + @@ -44,10 +41,18 @@ test_filename: test_dataflow_to_execution.py - + + + + + + + + + - + - + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_combined_metrics_plan__ep_0.xml index 2d133f4c6..24ab5848f 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_combined_metrics_plan__ep_0.xml @@ -5,82 +5,90 @@ test_filename: test_dataflow_to_execution.py - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_small_combined_metrics_plan__ep_0.xml index 4da40aaf0..1eff644ad 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_small_combined_metrics_plan__ep_0.xml @@ -17,11 +17,7 @@ test_filename: test_dataflow_to_execution.py - - - - - + @@ -32,8 +28,9 @@ test_filename: test_dataflow_to_execution.py - - + + + @@ -44,10 +41,18 @@ test_filename: test_dataflow_to_execution.py - + + + + + + + + + - + - + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_combined_metrics_plan__ep_0.xml index 2d133f4c6..24ab5848f 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_combined_metrics_plan__ep_0.xml @@ -5,82 +5,90 @@ test_filename: test_dataflow_to_execution.py - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_small_combined_metrics_plan__ep_0.xml index 4da40aaf0..1eff644ad 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_small_combined_metrics_plan__ep_0.xml @@ -17,11 +17,7 @@ test_filename: test_dataflow_to_execution.py - - - - - + @@ -32,8 +28,9 @@ test_filename: test_dataflow_to_execution.py - - + + + @@ -44,10 +41,18 @@ test_filename: test_dataflow_to_execution.py - + + + + + + + + + - + - + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_combined_metrics_plan__ep_0.xml index 2d133f4c6..24ab5848f 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_combined_metrics_plan__ep_0.xml @@ -5,82 +5,90 @@ test_filename: test_dataflow_to_execution.py - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_small_combined_metrics_plan__ep_0.xml index 4da40aaf0..1eff644ad 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_small_combined_metrics_plan__ep_0.xml @@ -17,11 +17,7 @@ test_filename: test_dataflow_to_execution.py - - - - - + @@ -32,8 +28,9 @@ test_filename: test_dataflow_to_execution.py - - + + + @@ -44,10 +41,18 @@ test_filename: test_dataflow_to_execution.py - + + + + + + + + + - + - + diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index dc98ebb10..12d0bb4a6 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -4,7 +4,7 @@ docstring: Tests the combine metrics node for ratio type metrics. sql_engine: BigQuery --- --- Compute Metrics via Expressions +-- Read From CTE For node_id=cm_2 WITH sma_28014_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' @@ -14,76 +14,90 @@ WITH sma_28014_cte AS ( FROM ***************************.dim_listings_latest listings_latest_src_28000 ) -SELECT - ds__day AS ds__day - , listing__country_latest AS listing__country_latest - , CAST(bookings AS FLOAT64) / CAST(NULLIF(views, 0) AS FLOAT64) AS bookings_per_view -FROM ( - -- Combine Aggregated Outputs +, cm_0_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_27.ds__day, subq_35.ds__day) AS ds__day - , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) AS listing__country_latest - , MAX(subq_27.bookings) AS bookings - , MAX(subq_35.views) AS views + subq_20.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_20.bookings) AS bookings FROM ( - -- Join Standard Outputs - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_20.bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS ds__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_20.listing = sma_28014_cte.listing - GROUP BY - ds__day - , listing__country_latest - ) subq_27 - FULL OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATETIME_TRUNC(ds, day) AS ds__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte + ON + subq_20.listing = sma_28014_cte.listing + GROUP BY + ds__day + , listing__country_latest +) + +, cm_1_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_29.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_29.views) AS views + FROM ( + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - subq_29.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_29.views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS ds__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_29.listing = sma_28014_cte.listing - GROUP BY - ds__day - , listing__country_latest - ) subq_35 + DATETIME_TRUNC(ds, day) AS ds__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte ON - ( - subq_27.listing__country_latest = subq_35.listing__country_latest - ) AND ( - subq_27.ds__day = subq_35.ds__day - ) + subq_29.listing = sma_28014_cte.listing GROUP BY ds__day , listing__country_latest -) subq_36 +) + +, cm_2_cte AS ( + -- Compute Metrics via Expressions + SELECT + ds__day + , listing__country_latest + , CAST(bookings AS FLOAT64) / CAST(NULLIF(views, 0) AS FLOAT64) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_0_cte.ds__day, cm_1_cte.ds__day) AS ds__day + , COALESCE(cm_0_cte.listing__country_latest, cm_1_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.views) AS views + FROM cm_0_cte cm_0_cte + FULL OUTER JOIN + cm_1_cte cm_1_cte + ON + ( + cm_0_cte.listing__country_latest = cm_1_cte.listing__country_latest + ) AND ( + cm_0_cte.ds__day = cm_1_cte.ds__day + ) + GROUP BY + ds__day + , listing__country_latest + ) subq_36 +) + +SELECT + ds__day AS ds__day + , listing__country_latest AS listing__country_latest + , bookings_per_view AS bookings_per_view +FROM cm_2_cte cm_2_cte diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index 45487cf27..1b0b0ce04 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -4,7 +4,7 @@ docstring: Tests the combine metrics node for ratio type metrics. sql_engine: Databricks --- --- Compute Metrics via Expressions +-- Read From CTE For node_id=cm_2 WITH sma_28014_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' @@ -14,76 +14,90 @@ WITH sma_28014_cte AS ( FROM ***************************.dim_listings_latest listings_latest_src_28000 ) -SELECT - ds__day AS ds__day - , listing__country_latest AS listing__country_latest - , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view -FROM ( - -- Combine Aggregated Outputs +, cm_0_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_27.ds__day, subq_35.ds__day) AS ds__day - , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) AS listing__country_latest - , MAX(subq_27.bookings) AS bookings - , MAX(subq_35.views) AS views + subq_20.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_20.bookings) AS bookings FROM ( - -- Join Standard Outputs - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_20.bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS ds__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_20.listing = sma_28014_cte.listing - GROUP BY - subq_20.ds__day - , sma_28014_cte.country_latest - ) subq_27 - FULL OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS ds__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte + ON + subq_20.listing = sma_28014_cte.listing + GROUP BY + subq_20.ds__day + , sma_28014_cte.country_latest +) + +, cm_1_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_29.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_29.views) AS views + FROM ( + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - subq_29.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_29.views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS ds__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_29.listing = sma_28014_cte.listing - GROUP BY - subq_29.ds__day - , sma_28014_cte.country_latest - ) subq_35 + DATE_TRUNC('day', ds) AS ds__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte ON - ( - subq_27.listing__country_latest = subq_35.listing__country_latest - ) AND ( - subq_27.ds__day = subq_35.ds__day - ) + subq_29.listing = sma_28014_cte.listing GROUP BY - COALESCE(subq_27.ds__day, subq_35.ds__day) - , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) -) subq_36 + subq_29.ds__day + , sma_28014_cte.country_latest +) + +, cm_2_cte AS ( + -- Compute Metrics via Expressions + SELECT + ds__day + , listing__country_latest + , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_0_cte.ds__day, cm_1_cte.ds__day) AS ds__day + , COALESCE(cm_0_cte.listing__country_latest, cm_1_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.views) AS views + FROM cm_0_cte cm_0_cte + FULL OUTER JOIN + cm_1_cte cm_1_cte + ON + ( + cm_0_cte.listing__country_latest = cm_1_cte.listing__country_latest + ) AND ( + cm_0_cte.ds__day = cm_1_cte.ds__day + ) + GROUP BY + COALESCE(cm_0_cte.ds__day, cm_1_cte.ds__day) + , COALESCE(cm_0_cte.listing__country_latest, cm_1_cte.listing__country_latest) + ) subq_36 +) + +SELECT + ds__day AS ds__day + , listing__country_latest AS listing__country_latest + , bookings_per_view AS bookings_per_view +FROM cm_2_cte cm_2_cte diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index c8d146de3..a03d15445 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -4,7 +4,7 @@ docstring: Tests the combine metrics node for ratio type metrics. sql_engine: Postgres --- --- Compute Metrics via Expressions +-- Read From CTE For node_id=cm_2 WITH sma_28014_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' @@ -14,76 +14,90 @@ WITH sma_28014_cte AS ( FROM ***************************.dim_listings_latest listings_latest_src_28000 ) -SELECT - ds__day AS ds__day - , listing__country_latest AS listing__country_latest - , CAST(bookings AS DOUBLE PRECISION) / CAST(NULLIF(views, 0) AS DOUBLE PRECISION) AS bookings_per_view -FROM ( - -- Combine Aggregated Outputs +, cm_0_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_27.ds__day, subq_35.ds__day) AS ds__day - , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) AS listing__country_latest - , MAX(subq_27.bookings) AS bookings - , MAX(subq_35.views) AS views + subq_20.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_20.bookings) AS bookings FROM ( - -- Join Standard Outputs - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_20.bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS ds__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_20.listing = sma_28014_cte.listing - GROUP BY - subq_20.ds__day - , sma_28014_cte.country_latest - ) subq_27 - FULL OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS ds__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte + ON + subq_20.listing = sma_28014_cte.listing + GROUP BY + subq_20.ds__day + , sma_28014_cte.country_latest +) + +, cm_1_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_29.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_29.views) AS views + FROM ( + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - subq_29.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_29.views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS ds__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_29.listing = sma_28014_cte.listing - GROUP BY - subq_29.ds__day - , sma_28014_cte.country_latest - ) subq_35 + DATE_TRUNC('day', ds) AS ds__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte ON - ( - subq_27.listing__country_latest = subq_35.listing__country_latest - ) AND ( - subq_27.ds__day = subq_35.ds__day - ) + subq_29.listing = sma_28014_cte.listing GROUP BY - COALESCE(subq_27.ds__day, subq_35.ds__day) - , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) -) subq_36 + subq_29.ds__day + , sma_28014_cte.country_latest +) + +, cm_2_cte AS ( + -- Compute Metrics via Expressions + SELECT + ds__day + , listing__country_latest + , CAST(bookings AS DOUBLE PRECISION) / CAST(NULLIF(views, 0) AS DOUBLE PRECISION) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_0_cte.ds__day, cm_1_cte.ds__day) AS ds__day + , COALESCE(cm_0_cte.listing__country_latest, cm_1_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.views) AS views + FROM cm_0_cte cm_0_cte + FULL OUTER JOIN + cm_1_cte cm_1_cte + ON + ( + cm_0_cte.listing__country_latest = cm_1_cte.listing__country_latest + ) AND ( + cm_0_cte.ds__day = cm_1_cte.ds__day + ) + GROUP BY + COALESCE(cm_0_cte.ds__day, cm_1_cte.ds__day) + , COALESCE(cm_0_cte.listing__country_latest, cm_1_cte.listing__country_latest) + ) subq_36 +) + +SELECT + ds__day AS ds__day + , listing__country_latest AS listing__country_latest + , bookings_per_view AS bookings_per_view +FROM cm_2_cte cm_2_cte diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index 3343128d7..27e3d1b3e 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -4,7 +4,7 @@ docstring: Tests the combine metrics node for ratio type metrics. sql_engine: Redshift --- --- Compute Metrics via Expressions +-- Read From CTE For node_id=cm_2 WITH sma_28014_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' @@ -14,76 +14,90 @@ WITH sma_28014_cte AS ( FROM ***************************.dim_listings_latest listings_latest_src_28000 ) -SELECT - ds__day AS ds__day - , listing__country_latest AS listing__country_latest - , CAST(bookings AS DOUBLE PRECISION) / CAST(NULLIF(views, 0) AS DOUBLE PRECISION) AS bookings_per_view -FROM ( - -- Combine Aggregated Outputs +, cm_0_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_27.ds__day, subq_35.ds__day) AS ds__day - , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) AS listing__country_latest - , MAX(subq_27.bookings) AS bookings - , MAX(subq_35.views) AS views + subq_20.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_20.bookings) AS bookings FROM ( - -- Join Standard Outputs - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_20.bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS ds__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_20.listing = sma_28014_cte.listing - GROUP BY - subq_20.ds__day - , sma_28014_cte.country_latest - ) subq_27 - FULL OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS ds__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte + ON + subq_20.listing = sma_28014_cte.listing + GROUP BY + subq_20.ds__day + , sma_28014_cte.country_latest +) + +, cm_1_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_29.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_29.views) AS views + FROM ( + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - subq_29.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_29.views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS ds__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_29.listing = sma_28014_cte.listing - GROUP BY - subq_29.ds__day - , sma_28014_cte.country_latest - ) subq_35 + DATE_TRUNC('day', ds) AS ds__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte ON - ( - subq_27.listing__country_latest = subq_35.listing__country_latest - ) AND ( - subq_27.ds__day = subq_35.ds__day - ) + subq_29.listing = sma_28014_cte.listing GROUP BY - COALESCE(subq_27.ds__day, subq_35.ds__day) - , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) -) subq_36 + subq_29.ds__day + , sma_28014_cte.country_latest +) + +, cm_2_cte AS ( + -- Compute Metrics via Expressions + SELECT + ds__day + , listing__country_latest + , CAST(bookings AS DOUBLE PRECISION) / CAST(NULLIF(views, 0) AS DOUBLE PRECISION) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_0_cte.ds__day, cm_1_cte.ds__day) AS ds__day + , COALESCE(cm_0_cte.listing__country_latest, cm_1_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.views) AS views + FROM cm_0_cte cm_0_cte + FULL OUTER JOIN + cm_1_cte cm_1_cte + ON + ( + cm_0_cte.listing__country_latest = cm_1_cte.listing__country_latest + ) AND ( + cm_0_cte.ds__day = cm_1_cte.ds__day + ) + GROUP BY + COALESCE(cm_0_cte.ds__day, cm_1_cte.ds__day) + , COALESCE(cm_0_cte.listing__country_latest, cm_1_cte.listing__country_latest) + ) subq_36 +) + +SELECT + ds__day AS ds__day + , listing__country_latest AS listing__country_latest + , bookings_per_view AS bookings_per_view +FROM cm_2_cte cm_2_cte diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index b14f947b5..1d65694e1 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -4,7 +4,7 @@ docstring: Tests the combine metrics node for ratio type metrics. sql_engine: Snowflake --- --- Compute Metrics via Expressions +-- Read From CTE For node_id=cm_2 WITH sma_28014_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' @@ -14,76 +14,90 @@ WITH sma_28014_cte AS ( FROM ***************************.dim_listings_latest listings_latest_src_28000 ) -SELECT - ds__day AS ds__day - , listing__country_latest AS listing__country_latest - , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view -FROM ( - -- Combine Aggregated Outputs +, cm_0_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_27.ds__day, subq_35.ds__day) AS ds__day - , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) AS listing__country_latest - , MAX(subq_27.bookings) AS bookings - , MAX(subq_35.views) AS views + subq_20.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_20.bookings) AS bookings FROM ( - -- Join Standard Outputs - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_20.bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS ds__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_20.listing = sma_28014_cte.listing - GROUP BY - subq_20.ds__day - , sma_28014_cte.country_latest - ) subq_27 - FULL OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS ds__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte + ON + subq_20.listing = sma_28014_cte.listing + GROUP BY + subq_20.ds__day + , sma_28014_cte.country_latest +) + +, cm_1_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_29.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_29.views) AS views + FROM ( + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - subq_29.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_29.views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS ds__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_29.listing = sma_28014_cte.listing - GROUP BY - subq_29.ds__day - , sma_28014_cte.country_latest - ) subq_35 + DATE_TRUNC('day', ds) AS ds__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte ON - ( - subq_27.listing__country_latest = subq_35.listing__country_latest - ) AND ( - subq_27.ds__day = subq_35.ds__day - ) + subq_29.listing = sma_28014_cte.listing GROUP BY - COALESCE(subq_27.ds__day, subq_35.ds__day) - , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) -) subq_36 + subq_29.ds__day + , sma_28014_cte.country_latest +) + +, cm_2_cte AS ( + -- Compute Metrics via Expressions + SELECT + ds__day + , listing__country_latest + , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_0_cte.ds__day, cm_1_cte.ds__day) AS ds__day + , COALESCE(cm_0_cte.listing__country_latest, cm_1_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.views) AS views + FROM cm_0_cte cm_0_cte + FULL OUTER JOIN + cm_1_cte cm_1_cte + ON + ( + cm_0_cte.listing__country_latest = cm_1_cte.listing__country_latest + ) AND ( + cm_0_cte.ds__day = cm_1_cte.ds__day + ) + GROUP BY + COALESCE(cm_0_cte.ds__day, cm_1_cte.ds__day) + , COALESCE(cm_0_cte.listing__country_latest, cm_1_cte.listing__country_latest) + ) subq_36 +) + +SELECT + ds__day AS ds__day + , listing__country_latest AS listing__country_latest + , bookings_per_view AS bookings_per_view +FROM cm_2_cte cm_2_cte diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index 84c74729c..87893cad7 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -4,7 +4,7 @@ docstring: Tests the combine metrics node for ratio type metrics. sql_engine: Trino --- --- Compute Metrics via Expressions +-- Read From CTE For node_id=cm_2 WITH sma_28014_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' @@ -14,76 +14,90 @@ WITH sma_28014_cte AS ( FROM ***************************.dim_listings_latest listings_latest_src_28000 ) -SELECT - ds__day AS ds__day - , listing__country_latest AS listing__country_latest - , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view -FROM ( - -- Combine Aggregated Outputs +, cm_0_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_27.ds__day, subq_35.ds__day) AS ds__day - , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) AS listing__country_latest - , MAX(subq_27.bookings) AS bookings - , MAX(subq_35.views) AS views + subq_20.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_20.bookings) AS bookings FROM ( - -- Join Standard Outputs - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_20.bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS ds__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_20.listing = sma_28014_cte.listing - GROUP BY - subq_20.ds__day - , sma_28014_cte.country_latest - ) subq_27 - FULL OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS ds__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte + ON + subq_20.listing = sma_28014_cte.listing + GROUP BY + subq_20.ds__day + , sma_28014_cte.country_latest +) + +, cm_1_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_29.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_29.views) AS views + FROM ( + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - subq_29.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_29.views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS ds__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_29.listing = sma_28014_cte.listing - GROUP BY - subq_29.ds__day - , sma_28014_cte.country_latest - ) subq_35 + DATE_TRUNC('day', ds) AS ds__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte ON - ( - subq_27.listing__country_latest = subq_35.listing__country_latest - ) AND ( - subq_27.ds__day = subq_35.ds__day - ) + subq_29.listing = sma_28014_cte.listing GROUP BY - COALESCE(subq_27.ds__day, subq_35.ds__day) - , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) -) subq_36 + subq_29.ds__day + , sma_28014_cte.country_latest +) + +, cm_2_cte AS ( + -- Compute Metrics via Expressions + SELECT + ds__day + , listing__country_latest + , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_0_cte.ds__day, cm_1_cte.ds__day) AS ds__day + , COALESCE(cm_0_cte.listing__country_latest, cm_1_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.views) AS views + FROM cm_0_cte cm_0_cte + FULL OUTER JOIN + cm_1_cte cm_1_cte + ON + ( + cm_0_cte.listing__country_latest = cm_1_cte.listing__country_latest + ) AND ( + cm_0_cte.ds__day = cm_1_cte.ds__day + ) + GROUP BY + COALESCE(cm_0_cte.ds__day, cm_1_cte.ds__day) + , COALESCE(cm_0_cte.listing__country_latest, cm_1_cte.listing__country_latest) + ) subq_36 +) + +SELECT + ds__day AS ds__day + , listing__country_latest AS listing__country_latest + , bookings_per_view AS bookings_per_view +FROM cm_2_cte cm_2_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql index f3a60c71e..64a2c46ed 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_cumulative_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Constrain Time Range to [2019-12-19T00:00:00, 2020-01-02T00:00:00] -- Pass Only Elements: ['bookers', 'metric_time__day'] @@ -42,4 +39,23 @@ FROM ( WHERE subq_17.metric_time__day BETWEEN '2019-12-19' AND '2020-01-02' GROUP BY metric_time__day -) subq_23 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric__plan0_optimized.sql index b50107ee6..d9c4c7c43 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -25,4 +22,24 @@ FROM ( ) subq_13 GROUP BY metric_time__day -) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , ref_bookings + , bookings + FROM cm_8_cte cm_8_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , non_referred_bookings_pct AS non_referred_bookings_pct +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql index 9269668a0..6796112fa 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql @@ -6,60 +6,81 @@ sql_engine: BigQuery --- -- Combine Aggregated Outputs -- Order By [] Limit 1 -SELECT - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) AS booking__is_instant - , MAX(subq_18.derived_shared_alias_1a) AS derived_shared_alias_1a - , MAX(subq_24.derived_shared_alias_2) AS derived_shared_alias_2 -FROM ( +WITH cm_8_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + GROUP BY + booking__is_instant +) + +, cm_9_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias - 10 AS derived_shared_alias_1a FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_8 SELECT booking__is_instant - , SUM(bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_8_cte cm_8_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_10_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(instant_bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_21 + GROUP BY + booking__is_instant +) + +, cm_11_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias + 10 AS derived_shared_alias_2 FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_10 SELECT booking__is_instant - , SUM(instant_bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_21 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_10_cte cm_10_cte ) subq_23 -) subq_24 +) + +SELECT + COALESCE(cm_9_cte.booking__is_instant, cm_11_cte.booking__is_instant) AS booking__is_instant + , MAX(cm_9_cte.derived_shared_alias_1a) AS derived_shared_alias_1a + , MAX(cm_11_cte.derived_shared_alias_2) AS derived_shared_alias_2 +FROM cm_9_cte cm_9_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.booking__is_instant = subq_24.booking__is_instant + cm_9_cte.booking__is_instant = cm_11_cte.booking__is_instant GROUP BY booking__is_instant LIMIT 1 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql index 787df638c..d5154984b 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_month_dimension_and_offset_window test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_last_month AS bookings_last_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] -- Aggregate Measures @@ -28,4 +25,23 @@ FROM ( DATE_SUB(CAST(subq_10.metric_time__month AS DATETIME), INTERVAL 1 month) = DATETIME_TRUNC(monthly_bookings_source_src_16000.ds, month) GROUP BY metric_time__month -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_last_month AS bookings_last_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_last_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_last_month AS bookings_last_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_to_grain__plan0_optimized.sql index a3b05090c..ee67d97e4 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_to_grain__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATETIME_TRUNC(subq_22.ds, month) = subq_20.metric_time__day - GROUP BY - metric_time__day - ) subq_26 + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + DATETIME_TRUNC(subq_22.ds, month) = subq_20.metric_time__day GROUP BY metric_time__day -) subq_27 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + metric_time__day + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql index 69b5a61e8..a88e1e084 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,59 +2,72 @@ test_name: test_derived_metric_with_offset_to_grain_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) AS metric_time__week - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__week + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__week - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__week'] - SELECT - DATETIME_TRUNC(ds, isoweek) AS metric_time__week - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__week - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__week'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATETIME_TRUNC(subq_22.ds, isoweek) AS metric_time__week - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATETIME_TRUNC(subq_22.ds, month) = subq_20.metric_time__day - WHERE DATETIME_TRUNC(subq_22.ds, isoweek) = subq_22.ds - GROUP BY - metric_time__week - ) subq_26 + DATETIME_TRUNC(ds, isoweek) AS metric_time__week + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__week +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__week'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATETIME_TRUNC(subq_22.ds, isoweek) AS metric_time__week + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__week = subq_26.metric_time__week + DATETIME_TRUNC(subq_22.ds, month) = subq_20.metric_time__day + WHERE DATETIME_TRUNC(subq_22.ds, isoweek) = subq_22.ds GROUP BY metric_time__week -) subq_27 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__week, cm_7_cte.metric_time__week) AS metric_time__week + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__week = cm_7_cte.metric_time__week + GROUP BY + metric_time__week + ) subq_27 +) + +SELECT + metric_time__week AS metric_time__week + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window__plan0_optimized.sql index fa0b0ca3b..7bad057b5 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_SUB(CAST(subq_22.ds AS DATETIME), INTERVAL 14 day) = subq_20.metric_time__day - GROUP BY - metric_time__day - ) subq_26 + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + DATE_SUB(CAST(subq_22.ds AS DATETIME), INTERVAL 14 day) = subq_20.metric_time__day GROUP BY metric_time__day -) subq_27 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + metric_time__day + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql index cc6350e6d..a814257dd 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__quarter - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) AS metric_time__quarter - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__quarter + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__quarter - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - SELECT - DATETIME_TRUNC(ds, quarter) AS metric_time__quarter - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__quarter - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATETIME_TRUNC(subq_22.ds, quarter) AS metric_time__quarter - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_SUB(CAST(subq_22.ds AS DATETIME), INTERVAL 14 day) = subq_20.metric_time__day - GROUP BY - metric_time__quarter - ) subq_26 + DATETIME_TRUNC(ds, quarter) AS metric_time__quarter + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__quarter +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__quarter'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATETIME_TRUNC(subq_22.ds, quarter) AS metric_time__quarter + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__quarter = subq_26.metric_time__quarter + DATE_SUB(CAST(subq_22.ds AS DATETIME), INTERVAL 14 day) = subq_20.metric_time__day GROUP BY metric_time__quarter -) subq_27 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__quarter + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__quarter, cm_7_cte.metric_time__quarter) AS metric_time__quarter + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__quarter = cm_7_cte.metric_time__quarter + GROUP BY + metric_time__quarter + ) subq_27 +) + +SELECT + metric_time__quarter AS metric_time__quarter + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql index a0e830b69..0252d5465 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql @@ -2,62 +2,75 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_20.ds AS metric_time__day + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds AS metric_time__day - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATETIME_TRUNC(subq_20.ds, month) = subq_18.metric_time__day - GROUP BY - metric_time__day - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATETIME_TRUNC(subq_20.ds, month) = subq_18.metric_time__day + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - DATE_SUB(CAST(subq_28.ds AS DATETIME), INTERVAL 1 month) = subq_26.metric_time__day - GROUP BY - metric_time__day - ) subq_32 + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__day = subq_32.metric_time__day + DATE_SUB(CAST(subq_28.ds AS DATETIME), INTERVAL 1 month) = subq_26.metric_time__day GROUP BY metric_time__day -) subq_33 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + metric_time__day + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql index 8f7ccace5..c7bcad791 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,63 +2,76 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain_and_granul test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__year - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) AS metric_time__year - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATETIME_TRUNC(subq_20.ds, year) AS metric_time__year + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATETIME_TRUNC(subq_20.ds, year) AS metric_time__year - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATETIME_TRUNC(subq_20.ds, month) = subq_18.metric_time__day - WHERE DATETIME_TRUNC(subq_20.ds, year) = subq_20.ds - GROUP BY - metric_time__year - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATETIME_TRUNC(subq_20.ds, month) = subq_18.metric_time__day + WHERE DATETIME_TRUNC(subq_20.ds, year) = subq_20.ds + GROUP BY + metric_time__year +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATETIME_TRUNC(subq_28.ds, year) AS metric_time__year + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATETIME_TRUNC(subq_28.ds, year) AS metric_time__year - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - DATE_SUB(CAST(subq_28.ds AS DATETIME), INTERVAL 1 month) = subq_26.metric_time__day - GROUP BY - metric_time__year - ) subq_32 + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__year = subq_32.metric_time__year + DATE_SUB(CAST(subq_28.ds AS DATETIME), INTERVAL 1 month) = subq_26.metric_time__day GROUP BY metric_time__year -) subq_33 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__year + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + GROUP BY + metric_time__year + ) subq_33 +) + +SELECT + metric_time__year AS metric_time__year + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql index 5fc91ceab..bdf6f5f3a 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_derived_metric_with_offset_window_and_time_filter test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_21.bookings) AS bookings - , MAX(subq_30.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_17 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(bookings) AS bookings_2_weeks_ago + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_25.ds AS metric_time__day + , subq_23.bookings AS bookings + FROM ***************************.mf_time_spine subq_25 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT DATETIME_TRUNC(ds, day) AS metric_time__day , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_17 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' - GROUP BY - metric_time__day - ) subq_21 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + ) subq_23 + ON + DATE_SUB(CAST(subq_25.ds AS DATETIME), INTERVAL 14 day) = subq_23.metric_time__day + ) subq_26 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_25.ds AS metric_time__day - , subq_23.bookings AS bookings - FROM ***************************.mf_time_spine subq_25 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - ON - DATE_SUB(CAST(subq_25.ds AS DATETIME), INTERVAL 14 day) = subq_23.metric_time__day - ) subq_26 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day GROUP BY metric_time__day - ) subq_30 - ON - subq_21.metric_time__day = subq_30.metric_time__day - GROUP BY - metric_time__day -) subq_31 + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_offset_cumulative_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_offset_cumulative_metric__plan0_optimized.sql index b66ab403f..14bde3724 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_offset_cumulative_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_offset_cumulative_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_cumulative_metric test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookers', 'metric_time__day'] -- Aggregate Measures @@ -34,4 +31,23 @@ FROM ( DATE_SUB(CAST(subq_17.ds AS DATETIME), INTERVAL 2 day) = subq_15.metric_time__day GROUP BY metric_time__day -) subq_21 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_21 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql index 3e333677d..ef48d6bdd 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql @@ -2,47 +2,60 @@ test_name: test_derived_offset_metric_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS booking__ds__day + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATE_SUB(CAST(subq_17.ds AS DATETIME), INTERVAL 1 week) = DATETIME_TRUNC(bookings_source_src_28000.ds, day) + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATETIME_TRUNC(ds, day) AS booking__ds__day + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + booking__ds__day +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + booking__ds__day + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS booking__ds__day - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - DATE_SUB(CAST(subq_17.ds AS DATETIME), INTERVAL 1 week) = DATETIME_TRUNC(bookings_source_src_28000.ds, day) - GROUP BY - booking__ds__day - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATETIME_TRUNC(ds, day) AS booking__ds__day - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day GROUP BY booking__ds__day - ) subq_26 - ON - subq_21.booking__ds__day = subq_26.booking__ds__day - GROUP BY - booking__ds__day -) subq_27 + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql index fcc4c1501..8fa6db602 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_metric_with_one_input_metric test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATE_SUB(CAST(subq_11.ds AS DATETIME), INTERVAL 5 day) = subq_9.metric_time__day GROUP BY metric_time__day -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_multi_metric_fill_null__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_multi_metric_fill_null__plan0_optimized.sql index efc35be36..ced7070ea 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_multi_metric_fill_null__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_multi_metric_fill_null__plan0_optimized.sql @@ -3,40 +3,45 @@ test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_16.metric_time__day, subq_21.metric_time__day) AS metric_time__day - , MAX(subq_16.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine - , MAX(subq_21.listings) AS listings -FROM ( +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - GROUP BY - metric_time__day - ) subq_14 + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 + GROUP BY + metric_time__day + ) subq_14 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_15 -) subq_16 -FULL OUTER JOIN ( +) + +, cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -53,8 +58,16 @@ FULL OUTER JOIN ( ) subq_19 GROUP BY metric_time__day -) subq_21 +) + +SELECT + COALESCE(cm_7_cte.metric_time__day, cm_8_cte.metric_time__day) AS metric_time__day + , MAX(cm_7_cte.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine + , MAX(cm_8_cte.listings) AS listings +FROM cm_7_cte cm_7_cte +FULL OUTER JOIN + cm_8_cte cm_8_cte ON - subq_16.metric_time__day = subq_21.metric_time__day + cm_7_cte.metric_time__day = cm_8_cte.metric_time__day GROUP BY metric_time__day diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_derived_metric__plan0_optimized.sql index c377f1c47..db3ca0a63 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_derived_metric__plan0_optimized.sql @@ -2,65 +2,87 @@ test_name: test_nested_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_17 +WITH cm_12_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_28.metric_time__day, subq_33.metric_time__day) AS metric_time__day - , MAX(subq_28.non_referred) AS non_referred - , MAX(subq_33.instant) AS instant - , MAX(subq_33.bookings) AS bookings + metric_time__day + , SUM(referred_bookings) AS ref_bookings + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred - FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(referred_bookings) AS ref_bookings - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - GROUP BY - metric_time__day - ) subq_27 - ) subq_28 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + GROUP BY + metric_time__day +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , SUM(instant_bookings) AS instant - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - GROUP BY - metric_time__day - ) subq_33 - ON - subq_28.metric_time__day = subq_33.metric_time__day + , ref_bookings + , bookings + FROM cm_12_cte cm_12_cte + ) subq_27 +) + +, cm_16_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(instant_bookings) AS instant + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 GROUP BY metric_time__day -) subq_34 +) + +, cm_17_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_13_cte.metric_time__day, cm_16_cte.metric_time__day) AS metric_time__day + , MAX(cm_13_cte.non_referred) AS non_referred + , MAX(cm_16_cte.instant) AS instant + , MAX(cm_16_cte.bookings) AS bookings + FROM cm_13_cte cm_13_cte + FULL OUTER JOIN + cm_16_cte cm_16_cte + ON + cm_13_cte.metric_time__day = cm_16_cte.metric_time__day + GROUP BY + metric_time__day + ) subq_34 +) + +SELECT + metric_time__day AS metric_time__day + , instant_plus_non_referred_bookings_pct AS instant_plus_non_referred_bookings_pct +FROM cm_17_cte cm_17_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql index 14f557c69..58961367a 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql @@ -2,57 +2,77 @@ test_name: test_nested_derived_metric_offset_with_joined_where_constraint_not_se test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT + subq_17.ds AS metric_time__day + , subq_15.booking__is_instant AS booking__is_instant + , SUM(subq_15.bookings) AS bookings + FROM ***************************.mf_time_spine subq_17 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + ON + DATE_SUB(CAST(subq_17.ds AS DATETIME), INTERVAL 5 day) = subq_15.metric_time__day + GROUP BY metric_time__day - , bookings_offset_once + , booking__is_instant +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking__is_instant + , 2 * bookings AS bookings_offset_once FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_6 SELECT - subq_24.ds AS metric_time__day - , subq_22.booking__is_instant AS booking__is_instant - , subq_22.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_24 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , booking__is_instant + , bookings + FROM cm_6_cte cm_6_cte + ) subq_21 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] + SELECT + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , booking__is_instant - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_17.ds AS metric_time__day - , subq_15.booking__is_instant AS booking__is_instant - , SUM(subq_15.bookings) AS bookings - FROM ***************************.mf_time_spine subq_17 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - ON - DATE_SUB(CAST(subq_17.ds AS DATETIME), INTERVAL 5 day) = subq_15.metric_time__day - GROUP BY - metric_time__day - , booking__is_instant - ) subq_21 - ) subq_22 - ON - DATE_SUB(CAST(subq_24.ds AS DATETIME), INTERVAL 2 day) = subq_22.metric_time__day - ) subq_25 - WHERE booking__is_instant -) subq_27 + subq_24.ds AS metric_time__day + , cm_7_cte.booking__is_instant AS booking__is_instant + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_24 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATE_SUB(CAST(subq_24.ds AS DATETIME), INTERVAL 2 day) = cm_7_cte.metric_time__day + ) subq_25 + WHERE booking__is_instant + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql index 07eb7ffae..204e2e60c 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql @@ -2,65 +2,95 @@ test_name: test_nested_derived_metric_with_offset_multiple_input_metrics test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_14 +WITH cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + metric_time__day +) + +, cm_11_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month - , MAX(subq_30.booking_fees) AS booking_fees + metric_time__day + , booking_value * 0.05 AS booking_fees_start_of_month FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_10 SELECT - subq_23.ds AS metric_time__day - , subq_21.booking_fees_start_of_month AS booking_fees_start_of_month - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions - SELECT - metric_time__day - , booking_value * 0.05 AS booking_fees_start_of_month - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - metric_time__day - ) subq_20 - ) subq_21 - ON - DATETIME_TRUNC(subq_23.ds, month) = subq_21.metric_time__day - ) subq_24 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , booking_value + FROM cm_10_cte cm_10_cte + ) subq_20 +) + +, cm_12_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + metric_time__day +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_value * 0.05 AS booking_fees + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , booking_value * 0.05 AS booking_fees + , booking_value + FROM cm_12_cte cm_12_cte + ) subq_29 +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_24.metric_time__day, cm_13_cte.metric_time__day) AS metric_time__day + , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month + , MAX(cm_13_cte.booking_fees) AS booking_fees FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - metric_time__day - ) subq_29 - ) subq_30 - ON - subq_24.metric_time__day = subq_30.metric_time__day - GROUP BY - metric_time__day -) subq_31 + subq_23.ds AS metric_time__day + , cm_11_cte.booking_fees_start_of_month AS booking_fees_start_of_month + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_11_cte cm_11_cte + ON + DATETIME_TRUNC(subq_23.ds, month) = cm_11_cte.metric_time__day + ) subq_24 + FULL OUTER JOIN + cm_13_cte cm_13_cte + ON + subq_24.metric_time__day = cm_13_cte.metric_time__day + GROUP BY + metric_time__day + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , booking_fees_since_start_of_month AS booking_fees_since_start_of_month +FROM cm_14_cte cm_14_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql index d915f0303..cf5dac388 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql @@ -2,36 +2,60 @@ test_name: test_nested_fill_nulls_without_time_spine test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_8 - GROUP BY - metric_time__day - ) subq_9 + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_8 + GROUP BY + metric_time__day + ) subq_9 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_10 -) subq_11 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_7_cte cm_7_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , nested_fill_nulls_without_time_spine AS nested_fill_nulls_without_time_spine +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql index 276d3da12..70090c447 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql @@ -3,46 +3,59 @@ test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_18.metric_time__day, subq_23.metric_time__day) AS metric_time__day - , MAX(subq_18.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_8_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine - FROM ( - -- Aggregate Measures - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_14 - GROUP BY - metric_time__day - ) subq_15 - ) subq_16 + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_14 + GROUP BY + metric_time__day + ) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_8_cte cm_8_cte + ) subq_16 +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_9 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_9_cte cm_9_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_11_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -59,8 +72,16 @@ FULL OUTER JOIN ( ) subq_21 GROUP BY metric_time__day -) subq_23 +) + +SELECT + COALESCE(cm_10_cte.metric_time__day, cm_11_cte.metric_time__day) AS metric_time__day + , MAX(cm_10_cte.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine + , MAX(cm_11_cte.listings) AS listings +FROM cm_10_cte cm_10_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.metric_time__day = subq_23.metric_time__day + cm_10_cte.metric_time__day = cm_11_cte.metric_time__day GROUP BY metric_time__day diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_filters__plan0_optimized.sql index 0ba29d991..d04cc125e 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_filters__plan0_optimized.sql @@ -4,67 +4,86 @@ docstring: Tests derived metric rendering for a nested derived metric with filters on the outer metric spec. sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - instant_lux_booking_value_rate AS instant_lux_booking_value_rate -FROM ( +-- Read From CTE For node_id=cm_15 +WITH cm_13_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings'] + -- Aggregate Measures -- Compute Metrics via Expressions SELECT - average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Combine Aggregated Outputs + -- Join Standard Outputs SELECT - MAX(subq_37.average_booking_value) AS average_booking_value - , MAX(subq_37.bookings) AS bookings - , MAX(subq_43.booking_value) AS booking_value + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_29.booking__is_instant AS booking__is_instant + , subq_29.bookings AS bookings + , subq_29.average_booking_value AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_29.booking__is_instant AS booking__is_instant - , subq_29.bookings AS bookings - , subq_29.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_29.listing = listings_latest_src_28000.listing_id - ) subq_33 - WHERE (listing__is_lux_latest) AND (booking__is_instant) - ) subq_37 - CROSS JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value',] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - SUM(booking_value) AS booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_39 - WHERE booking__is_instant - ) subq_43 + listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_29.listing = listings_latest_src_28000.listing_id + ) subq_33 + WHERE (listing__is_lux_latest) AND (booking__is_instant) +) + +, cm_12_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(booking_value) AS booking_value + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + is_instant AS booking__is_instant + , booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_39 + WHERE booking__is_instant +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(cm_13_cte.average_booking_value) AS average_booking_value + , MAX(cm_13_cte.bookings) AS bookings + , MAX(cm_12_cte.booking_value) AS booking_value + FROM cm_13_cte cm_13_cte + CROSS JOIN + cm_12_cte cm_12_cte ) subq_44 -) subq_45 +) + +, cm_15_cte AS ( + -- Compute Metrics via Expressions + SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate + FROM ( + -- Read From CTE For node_id=cm_14 + SELECT + instant_lux_booking_value_rate + FROM cm_14_cte cm_14_cte + ) subq_45 +) + +SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate +FROM cm_15_cte cm_15_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_offsets__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_offsets__plan0_optimized.sql index 14d59bc46..336c997aa 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_offsets__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_offsets__plan0_optimized.sql @@ -2,44 +2,63 @@ test_name: test_nested_offsets test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_22 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - ON - DATE_SUB(CAST(subq_15.ds AS DATETIME), INTERVAL 5 day) = subq_13.metric_time__day - GROUP BY - metric_time__day - ) subq_19 - ) subq_20 + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 ON - DATE_SUB(CAST(subq_22.ds AS DATETIME), INTERVAL 2 day) = subq_20.metric_time__day -) subq_23 + DATE_SUB(CAST(subq_15.ds AS DATETIME), INTERVAL 5 day) = subq_13.metric_time__day + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_22.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_22 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATE_SUB(CAST(subq_22.ds AS DATETIME), INTERVAL 2 day) = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_offsets_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_offsets_with_time_constraint__plan0_optimized.sql index f5654357b..78505ec3f 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_offsets_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_offsets_with_time_constraint__plan0_optimized.sql @@ -2,50 +2,69 @@ test_name: test_nested_offsets_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.metric_time__day AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ( - -- Time Spine - SELECT - ds AS metric_time__day - FROM ***************************.mf_time_spine subq_22 - WHERE ds BETWEEN '2020-01-12' AND '2020-01-13' - ) subq_21 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 + ON + DATE_SUB(CAST(subq_15.ds AS DATETIME), INTERVAL 5 day) = subq_13.metric_time__day + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 SELECT metric_time__day - , 2 * bookings AS bookings_offset_once + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_21.metric_time__day AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Time Spine SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - ON - DATE_SUB(CAST(subq_15.ds AS DATETIME), INTERVAL 5 day) = subq_13.metric_time__day - GROUP BY - metric_time__day - ) subq_19 - ) subq_20 - ON - DATE_SUB(CAST(subq_21.metric_time__day AS DATETIME), INTERVAL 2 day) = subq_20.metric_time__day -) subq_23 + ds AS metric_time__day + FROM ***************************.mf_time_spine subq_22 + WHERE ds BETWEEN '2020-01-12' AND '2020-01-13' + ) subq_21 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATE_SUB(CAST(subq_21.metric_time__day AS DATETIME), INTERVAL 2 day) = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_offsets_with_where_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_offsets_with_where_constraint__plan0_optimized.sql index 974fba188..d2b729fa7 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_offsets_with_where_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_offsets_with_where_constraint__plan0_optimized.sql @@ -2,51 +2,70 @@ test_name: test_nested_offsets_with_where_constraint test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT + subq_16.ds AS metric_time__day + , SUM(subq_14.bookings) AS bookings + FROM ***************************.mf_time_spine subq_16 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_14 + ON + DATE_SUB(CAST(subq_16.ds AS DATETIME), INTERVAL 5 day) = subq_14.metric_time__day + GROUP BY metric_time__day - , bookings_offset_once +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_6 SELECT - subq_23.ds AS metric_time__day - , subq_21.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_20 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Constrain Output with WHERE + SELECT + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_16.ds AS metric_time__day - , SUM(subq_14.bookings) AS bookings - FROM ***************************.mf_time_spine subq_16 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_14 - ON - DATE_SUB(CAST(subq_16.ds AS DATETIME), INTERVAL 5 day) = subq_14.metric_time__day - GROUP BY - metric_time__day - ) subq_20 - ) subq_21 - ON - DATE_SUB(CAST(subq_23.ds AS DATETIME), INTERVAL 2 day) = subq_21.metric_time__day - ) subq_24 - WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' -) subq_25 + subq_23.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATE_SUB(CAST(subq_23.ds AS DATETIME), INTERVAL 2 day) = cm_7_cte.metric_time__day + ) subq_24 + WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' + ) subq_25 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index 9b2141086..ad0abbefe 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,11 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with one granularity and filtered by a different one. sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__month'] -- Aggregate Measures @@ -38,4 +35,23 @@ FROM ( WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__month -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql index 039f084b2..64ae5c216 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql @@ -4,13 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with multiple granularities. sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day', 'metric_time__month', 'metric_time__year'] -- Aggregate Measures @@ -35,4 +30,29 @@ FROM ( metric_time__day , metric_time__month , metric_time__year -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql index fb7460574..cf7b8b2be 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_to_grain_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATETIME_TRUNC(ds, day) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATETIME_TRUNC(subq_22.ds, month) = subq_20.booking__ds__day - GROUP BY - booking__ds__day - ) subq_26 + DATETIME_TRUNC(ds, day) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + DATETIME_TRUNC(subq_22.ds, month) = subq_20.booking__ds__day GROUP BY booking__ds__day -) subq_27 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + booking__ds__day + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index 73536fe94..7e4989455 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,63 +4,76 @@ docstring: Test a query where an offset window metric is queried with one granularity and filtered by a different one. sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) AS metric_time__month - , MAX(subq_24.booking_value) AS booking_value - , MAX(subq_30.bookers) AS bookers + metric_time__month + , SUM(booking_value) AS booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__month - , SUM(booking_value) AS booking_value - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_19.ds AS metric_time__day - , DATETIME_TRUNC(subq_19.ds, month) AS metric_time__month - , bookings_source_src_28000.booking_value AS booking_value - FROM ***************************.mf_time_spine subq_19 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 - ON - DATE_SUB(CAST(subq_19.ds AS DATETIME), INTERVAL 1 week) = DATETIME_TRUNC(bookings_source_src_28000.ds, day) - ) subq_20 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_24 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookers', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_19.ds AS metric_time__day + , DATETIME_TRUNC(subq_19.ds, month) AS metric_time__month + , bookings_source_src_28000.booking_value AS booking_value + FROM ***************************.mf_time_spine subq_19 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATE_SUB(CAST(subq_19.ds AS DATETIME), INTERVAL 1 week) = DATETIME_TRUNC(bookings_source_src_28000.ds, day) + ) subq_20 + WHERE metric_time__day = '2020-01-01' + GROUP BY + metric_time__month +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__month + , COUNT(DISTINCT bookers) AS bookers + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__month - , COUNT(DISTINCT bookers) AS bookers - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , DATETIME_TRUNC(ds, month) AS metric_time__month - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_30 - ON - subq_24.metric_time__month = subq_30.metric_time__month + DATETIME_TRUNC(ds, day) AS metric_time__day + , DATETIME_TRUNC(ds, month) AS metric_time__month + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__month -) subq_31 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + GROUP BY + metric_time__month + ) subq_31 +) + +SELECT + metric_time__month AS metric_time__month + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_metric_multiple_granularities__plan0_optimized.sql index a913e59a6..283e3cb8e 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_metric_multiple_granularities__plan0_optimized.sql @@ -4,67 +4,82 @@ docstring: Test a query where an offset window metric is queried with multiple granularities. sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS metric_time__day + , DATETIME_TRUNC(subq_17.ds, month) AS metric_time__month + , DATETIME_TRUNC(subq_17.ds, year) AS metric_time__year + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATE_SUB(CAST(subq_17.ds AS DATETIME), INTERVAL 1 week) = DATETIME_TRUNC(bookings_source_src_28000.ds, day) + GROUP BY + metric_time__day + , metric_time__month + , metric_time__year +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) AS metric_time__month - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) AS metric_time__year - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + DATETIME_TRUNC(ds, day) AS metric_time__day + , DATETIME_TRUNC(ds, month) AS metric_time__month + , DATETIME_TRUNC(ds, year) AS metric_time__year + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + metric_time__day + , metric_time__month + , metric_time__year +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS metric_time__day - , DATETIME_TRUNC(subq_17.ds, month) AS metric_time__month - , DATETIME_TRUNC(subq_17.ds, year) AS metric_time__year - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - DATE_SUB(CAST(subq_17.ds AS DATETIME), INTERVAL 1 week) = DATETIME_TRUNC(bookings_source_src_28000.ds, day) - GROUP BY - metric_time__day - , metric_time__month - , metric_time__year - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , DATETIME_TRUNC(ds, month) AS metric_time__month - , DATETIME_TRUNC(ds, year) AS metric_time__year - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 + ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) AND ( + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + ) AND ( + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + ) GROUP BY metric_time__day , metric_time__month , metric_time__year - ) subq_26 - ON - ( - subq_21.metric_time__day = subq_26.metric_time__day - ) AND ( - subq_21.metric_time__month = subq_26.metric_time__month - ) AND ( - subq_21.metric_time__year = subq_26.metric_time__year - ) - GROUP BY - metric_time__day - , metric_time__month - , metric_time__year -) subq_27 + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_with_agg_time_dim__plan0_optimized.sql index 201002c96..23c7fc595 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATETIME_TRUNC(ds, day) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_SUB(CAST(subq_22.ds AS DATETIME), INTERVAL 14 day) = subq_20.booking__ds__day - GROUP BY - booking__ds__day - ) subq_26 + DATETIME_TRUNC(ds, day) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + DATE_SUB(CAST(subq_22.ds AS DATETIME), INTERVAL 14 day) = subq_20.booking__ds__day GROUP BY booking__ds__day -) subq_27 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + booking__ds__day + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_time_offset_metric_with_time_constraint__plan0_optimized.sql index af157170a..1c17fa88d 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -33,4 +30,23 @@ FROM ( DATE_SUB(CAST(subq_10.metric_time__day AS DATETIME), INTERVAL 5 day) = subq_9.metric_time__day GROUP BY metric_time__day -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql index 70087237b..db6f04e80 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_cumulative_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Constrain Time Range to [2019-12-19T00:00:00, 2020-01-02T00:00:00] -- Pass Only Elements: ['bookers', 'metric_time__day'] @@ -42,4 +39,23 @@ FROM ( WHERE subq_17.metric_time__day BETWEEN '2019-12-19' AND '2020-01-02' GROUP BY subq_17.metric_time__day -) subq_23 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric__plan0_optimized.sql index a8fb9b798..205ca6992 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -25,4 +22,24 @@ FROM ( ) subq_13 GROUP BY metric_time__day -) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , ref_bookings + , bookings + FROM cm_8_cte cm_8_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , non_referred_bookings_pct AS non_referred_bookings_pct +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql index 456fd4087..471e57d5f 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql @@ -6,60 +6,81 @@ sql_engine: Databricks --- -- Combine Aggregated Outputs -- Order By [] Limit 1 -SELECT - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) AS booking__is_instant - , MAX(subq_18.derived_shared_alias_1a) AS derived_shared_alias_1a - , MAX(subq_24.derived_shared_alias_2) AS derived_shared_alias_2 -FROM ( +WITH cm_8_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + GROUP BY + booking__is_instant +) + +, cm_9_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias - 10 AS derived_shared_alias_1a FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_8 SELECT booking__is_instant - , SUM(bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_8_cte cm_8_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_10_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(instant_bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_21 + GROUP BY + booking__is_instant +) + +, cm_11_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias + 10 AS derived_shared_alias_2 FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_10 SELECT booking__is_instant - , SUM(instant_bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_21 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_10_cte cm_10_cte ) subq_23 -) subq_24 +) + +SELECT + COALESCE(cm_9_cte.booking__is_instant, cm_11_cte.booking__is_instant) AS booking__is_instant + , MAX(cm_9_cte.derived_shared_alias_1a) AS derived_shared_alias_1a + , MAX(cm_11_cte.derived_shared_alias_2) AS derived_shared_alias_2 +FROM cm_9_cte cm_9_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.booking__is_instant = subq_24.booking__is_instant + cm_9_cte.booking__is_instant = cm_11_cte.booking__is_instant GROUP BY - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) + COALESCE(cm_9_cte.booking__is_instant, cm_11_cte.booking__is_instant) LIMIT 1 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql index 6c0fe0c3e..95a98e6aa 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_month_dimension_and_offset_window test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_last_month AS bookings_last_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] -- Aggregate Measures @@ -28,4 +25,23 @@ FROM ( DATEADD(month, -1, subq_10.metric_time__month) = DATE_TRUNC('month', monthly_bookings_source_src_16000.ds) GROUP BY subq_10.metric_time__month -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_last_month AS bookings_last_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_last_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_last_month AS bookings_last_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_to_grain__plan0_optimized.sql index 1e504f020..ba7c890c9 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_to_grain__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql index 0422c7745..b7ccc6fc6 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,59 +2,72 @@ test_name: test_derived_metric_with_offset_to_grain_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) AS metric_time__week - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__week + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__week - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__week'] - SELECT - DATE_TRUNC('week', ds) AS metric_time__week - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__week - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__week'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATE_TRUNC('week', subq_22.ds) AS metric_time__week - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day - WHERE DATE_TRUNC('week', subq_22.ds) = subq_22.ds - GROUP BY - DATE_TRUNC('week', subq_22.ds) - ) subq_26 + DATE_TRUNC('week', ds) AS metric_time__week + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__week +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__week'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('week', subq_22.ds) AS metric_time__week + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__week = subq_26.metric_time__week + DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day + WHERE DATE_TRUNC('week', subq_22.ds) = subq_22.ds GROUP BY - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) -) subq_27 + DATE_TRUNC('week', subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__week, cm_7_cte.metric_time__week) AS metric_time__week + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__week = cm_7_cte.metric_time__week + GROUP BY + COALESCE(cm_6_cte.metric_time__week, cm_7_cte.metric_time__week) + ) subq_27 +) + +SELECT + metric_time__week AS metric_time__week + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window__plan0_optimized.sql index cf81e784d..7921c172f 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql index f38cb7d2b..c4c65cd1d 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__quarter - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) AS metric_time__quarter - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__quarter + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__quarter - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - SELECT - DATE_TRUNC('quarter', ds) AS metric_time__quarter - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__quarter - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATE_TRUNC('quarter', subq_22.ds) AS metric_time__quarter - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day - GROUP BY - DATE_TRUNC('quarter', subq_22.ds) - ) subq_26 + DATE_TRUNC('quarter', ds) AS metric_time__quarter + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__quarter +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__quarter'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('quarter', subq_22.ds) AS metric_time__quarter + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__quarter = subq_26.metric_time__quarter + DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) -) subq_27 + DATE_TRUNC('quarter', subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__quarter + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__quarter, cm_7_cte.metric_time__quarter) AS metric_time__quarter + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__quarter = cm_7_cte.metric_time__quarter + GROUP BY + COALESCE(cm_6_cte.metric_time__quarter, cm_7_cte.metric_time__quarter) + ) subq_27 +) + +SELECT + metric_time__quarter AS metric_time__quarter + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql index be9642ae8..e0a5b0cdd 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql @@ -2,62 +2,75 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_20.ds AS metric_time__day + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds AS metric_time__day - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day - GROUP BY - subq_20.ds - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day + GROUP BY + subq_20.ds +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - DATEADD(month, -1, subq_28.ds) = subq_26.metric_time__day - GROUP BY - subq_28.ds - ) subq_32 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__day = subq_32.metric_time__day + DATEADD(month, -1, subq_28.ds) = subq_26.metric_time__day GROUP BY - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) -) subq_33 + subq_28.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql index 55bf083f4..4392fcc65 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,63 +2,76 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain_and_granul test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__year - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) AS metric_time__year - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('year', subq_20.ds) AS metric_time__year + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATE_TRUNC('year', subq_20.ds) AS metric_time__year - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day - WHERE DATE_TRUNC('year', subq_20.ds) = subq_20.ds - GROUP BY - DATE_TRUNC('year', subq_20.ds) - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day + WHERE DATE_TRUNC('year', subq_20.ds) = subq_20.ds + GROUP BY + DATE_TRUNC('year', subq_20.ds) +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('year', subq_28.ds) AS metric_time__year + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATE_TRUNC('year', subq_28.ds) AS metric_time__year - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - DATEADD(month, -1, subq_28.ds) = subq_26.metric_time__day - GROUP BY - DATE_TRUNC('year', subq_28.ds) - ) subq_32 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__year = subq_32.metric_time__year + DATEADD(month, -1, subq_28.ds) = subq_26.metric_time__day GROUP BY - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) -) subq_33 + DATE_TRUNC('year', subq_28.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__year + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + GROUP BY + COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) + ) subq_33 +) + +SELECT + metric_time__year AS metric_time__year + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql index f5faf4483..310e9489f 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_derived_metric_with_offset_window_and_time_filter test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_17 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_21.bookings) AS bookings - , MAX(subq_30.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings_2_weeks_ago FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( + subq_25.ds AS metric_time__day + , subq_23.bookings AS bookings + FROM ***************************.mf_time_spine subq_25 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT DATE_TRUNC('day', ds) AS metric_time__day , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_17 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' - GROUP BY - metric_time__day - ) subq_21 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + ) subq_23 + ON + DATEADD(day, -14, subq_25.ds) = subq_23.metric_time__day + ) subq_26 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_25.ds AS metric_time__day - , subq_23.bookings AS bookings - FROM ***************************.mf_time_spine subq_25 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - ON - DATEADD(day, -14, subq_25.ds) = subq_23.metric_time__day - ) subq_26 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day GROUP BY - metric_time__day - ) subq_30 - ON - subq_21.metric_time__day = subq_30.metric_time__day - GROUP BY - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) -) subq_31 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_offset_cumulative_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_offset_cumulative_metric__plan0_optimized.sql index 46f8a04db..9c62a830f 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_offset_cumulative_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_offset_cumulative_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_cumulative_metric test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookers', 'metric_time__day'] -- Aggregate Measures @@ -34,4 +31,23 @@ FROM ( DATEADD(day, -2, subq_17.ds) = subq_15.metric_time__day GROUP BY subq_17.ds -) subq_21 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_21 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql index 78ae26a9e..9b1d0d166 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql @@ -2,47 +2,60 @@ test_name: test_derived_offset_metric_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS booking__ds__day + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATEADD(week, -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + GROUP BY + subq_17.ds +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + booking__ds__day + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS booking__ds__day - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - DATEADD(week, -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day GROUP BY - subq_17.ds - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_26 - ON - subq_21.booking__ds__day = subq_26.booking__ds__day - GROUP BY - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql index ef9112432..59e7df52a 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_metric_with_one_input_metric test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATEADD(day, -5, subq_11.ds) = subq_9.metric_time__day GROUP BY subq_11.ds -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_multi_metric_fill_null__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_multi_metric_fill_null__plan0_optimized.sql index e962df7e3..243527b91 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_multi_metric_fill_null__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_multi_metric_fill_null__plan0_optimized.sql @@ -3,40 +3,45 @@ test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_16.metric_time__day, subq_21.metric_time__day) AS metric_time__day - , MAX(subq_16.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine - , MAX(subq_21.listings) AS listings -FROM ( +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - GROUP BY - metric_time__day - ) subq_14 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 + GROUP BY + metric_time__day + ) subq_14 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_15 -) subq_16 -FULL OUTER JOIN ( +) + +, cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -53,8 +58,16 @@ FULL OUTER JOIN ( ) subq_19 GROUP BY metric_time__day -) subq_21 +) + +SELECT + COALESCE(cm_7_cte.metric_time__day, cm_8_cte.metric_time__day) AS metric_time__day + , MAX(cm_7_cte.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine + , MAX(cm_8_cte.listings) AS listings +FROM cm_7_cte cm_7_cte +FULL OUTER JOIN + cm_8_cte cm_8_cte ON - subq_16.metric_time__day = subq_21.metric_time__day + cm_7_cte.metric_time__day = cm_8_cte.metric_time__day GROUP BY - COALESCE(subq_16.metric_time__day, subq_21.metric_time__day) + COALESCE(cm_7_cte.metric_time__day, cm_8_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_derived_metric__plan0_optimized.sql index 25edde45c..393f08d95 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_derived_metric__plan0_optimized.sql @@ -2,65 +2,87 @@ test_name: test_nested_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_17 +WITH cm_12_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_28.metric_time__day, subq_33.metric_time__day) AS metric_time__day - , MAX(subq_28.non_referred) AS non_referred - , MAX(subq_33.instant) AS instant - , MAX(subq_33.bookings) AS bookings + metric_time__day + , SUM(referred_bookings) AS ref_bookings + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred - FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(referred_bookings) AS ref_bookings - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - GROUP BY - metric_time__day - ) subq_27 - ) subq_28 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + GROUP BY + metric_time__day +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , SUM(instant_bookings) AS instant - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - GROUP BY - metric_time__day - ) subq_33 - ON - subq_28.metric_time__day = subq_33.metric_time__day + , ref_bookings + , bookings + FROM cm_12_cte cm_12_cte + ) subq_27 +) + +, cm_16_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(instant_bookings) AS instant + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 GROUP BY - COALESCE(subq_28.metric_time__day, subq_33.metric_time__day) -) subq_34 + metric_time__day +) + +, cm_17_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_13_cte.metric_time__day, cm_16_cte.metric_time__day) AS metric_time__day + , MAX(cm_13_cte.non_referred) AS non_referred + , MAX(cm_16_cte.instant) AS instant + , MAX(cm_16_cte.bookings) AS bookings + FROM cm_13_cte cm_13_cte + FULL OUTER JOIN + cm_16_cte cm_16_cte + ON + cm_13_cte.metric_time__day = cm_16_cte.metric_time__day + GROUP BY + COALESCE(cm_13_cte.metric_time__day, cm_16_cte.metric_time__day) + ) subq_34 +) + +SELECT + metric_time__day AS metric_time__day + , instant_plus_non_referred_bookings_pct AS instant_plus_non_referred_bookings_pct +FROM cm_17_cte cm_17_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql index 6e2e83b47..704ae2b35 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql @@ -2,57 +2,77 @@ test_name: test_nested_derived_metric_offset_with_joined_where_constraint_not_se test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS metric_time__day + , subq_15.booking__is_instant AS booking__is_instant + , SUM(subq_15.bookings) AS bookings + FROM ***************************.mf_time_spine subq_17 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + ON + DATEADD(day, -5, subq_17.ds) = subq_15.metric_time__day + GROUP BY + subq_17.ds + , subq_15.booking__is_instant +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking__is_instant + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , booking__is_instant + , bookings + FROM cm_6_cte cm_6_cte + ) subq_21 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__day - , bookings_offset_once + , 2 * bookings_offset_once AS bookings_offset_twice FROM ( - -- Join to Time Spine Dataset + -- Constrain Output with WHERE + -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] SELECT - subq_24.ds AS metric_time__day - , subq_22.booking__is_instant AS booking__is_instant - , subq_22.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_24 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , booking__is_instant - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_17.ds AS metric_time__day - , subq_15.booking__is_instant AS booking__is_instant - , SUM(subq_15.bookings) AS bookings - FROM ***************************.mf_time_spine subq_17 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - ON - DATEADD(day, -5, subq_17.ds) = subq_15.metric_time__day - GROUP BY - subq_17.ds - , subq_15.booking__is_instant - ) subq_21 - ) subq_22 - ON - DATEADD(day, -2, subq_24.ds) = subq_22.metric_time__day - ) subq_25 - WHERE booking__is_instant -) subq_27 + subq_24.ds AS metric_time__day + , cm_7_cte.booking__is_instant AS booking__is_instant + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_24 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATEADD(day, -2, subq_24.ds) = cm_7_cte.metric_time__day + ) subq_25 + WHERE booking__is_instant + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql index 3c7b7bd4b..64e71d72f 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql @@ -2,65 +2,95 @@ test_name: test_nested_derived_metric_with_offset_multiple_input_metrics test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_14 +WITH cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_11_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month - , MAX(subq_30.booking_fees) AS booking_fees + metric_time__day + , booking_value * 0.05 AS booking_fees_start_of_month FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_10 SELECT - subq_23.ds AS metric_time__day - , subq_21.booking_fees_start_of_month AS booking_fees_start_of_month - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions - SELECT - metric_time__day - , booking_value * 0.05 AS booking_fees_start_of_month - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_20 - ) subq_21 - ON - DATE_TRUNC('month', subq_23.ds) = subq_21.metric_time__day - ) subq_24 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , booking_value + FROM cm_10_cte cm_10_cte + ) subq_20 +) + +, cm_12_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_value * 0.05 AS booking_fees + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , booking_value * 0.05 AS booking_fees + , booking_value + FROM cm_12_cte cm_12_cte + ) subq_29 +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_24.metric_time__day, cm_13_cte.metric_time__day) AS metric_time__day + , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month + , MAX(cm_13_cte.booking_fees) AS booking_fees FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_29 - ) subq_30 - ON - subq_24.metric_time__day = subq_30.metric_time__day - GROUP BY - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) -) subq_31 + subq_23.ds AS metric_time__day + , cm_11_cte.booking_fees_start_of_month AS booking_fees_start_of_month + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_11_cte cm_11_cte + ON + DATE_TRUNC('month', subq_23.ds) = cm_11_cte.metric_time__day + ) subq_24 + FULL OUTER JOIN + cm_13_cte cm_13_cte + ON + subq_24.metric_time__day = cm_13_cte.metric_time__day + GROUP BY + COALESCE(subq_24.metric_time__day, cm_13_cte.metric_time__day) + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , booking_fees_since_start_of_month AS booking_fees_since_start_of_month +FROM cm_14_cte cm_14_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql index dc2f86e8b..5c219c409 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql @@ -2,36 +2,60 @@ test_name: test_nested_fill_nulls_without_time_spine test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_8 - GROUP BY - metric_time__day - ) subq_9 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_8 + GROUP BY + metric_time__day + ) subq_9 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_10 -) subq_11 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_7_cte cm_7_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , nested_fill_nulls_without_time_spine AS nested_fill_nulls_without_time_spine +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql index 7848e3fc5..e48abb354 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql @@ -3,46 +3,59 @@ test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_18.metric_time__day, subq_23.metric_time__day) AS metric_time__day - , MAX(subq_18.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_8_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine - FROM ( - -- Aggregate Measures - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_14 - GROUP BY - metric_time__day - ) subq_15 - ) subq_16 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_14 + GROUP BY + metric_time__day + ) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_8_cte cm_8_cte + ) subq_16 +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_9 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_9_cte cm_9_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_11_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -59,8 +72,16 @@ FULL OUTER JOIN ( ) subq_21 GROUP BY metric_time__day -) subq_23 +) + +SELECT + COALESCE(cm_10_cte.metric_time__day, cm_11_cte.metric_time__day) AS metric_time__day + , MAX(cm_10_cte.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine + , MAX(cm_11_cte.listings) AS listings +FROM cm_10_cte cm_10_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.metric_time__day = subq_23.metric_time__day + cm_10_cte.metric_time__day = cm_11_cte.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_23.metric_time__day) + COALESCE(cm_10_cte.metric_time__day, cm_11_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_filters__plan0_optimized.sql index 1e23fe406..fefcc1afc 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_filters__plan0_optimized.sql @@ -4,67 +4,86 @@ docstring: Tests derived metric rendering for a nested derived metric with filters on the outer metric spec. sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - instant_lux_booking_value_rate AS instant_lux_booking_value_rate -FROM ( +-- Read From CTE For node_id=cm_15 +WITH cm_13_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings'] + -- Aggregate Measures -- Compute Metrics via Expressions SELECT - average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Combine Aggregated Outputs + -- Join Standard Outputs SELECT - MAX(subq_37.average_booking_value) AS average_booking_value - , MAX(subq_37.bookings) AS bookings - , MAX(subq_43.booking_value) AS booking_value + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_29.booking__is_instant AS booking__is_instant + , subq_29.bookings AS bookings + , subq_29.average_booking_value AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_29.booking__is_instant AS booking__is_instant - , subq_29.bookings AS bookings - , subq_29.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_29.listing = listings_latest_src_28000.listing_id - ) subq_33 - WHERE (listing__is_lux_latest) AND (booking__is_instant) - ) subq_37 - CROSS JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value',] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - SUM(booking_value) AS booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_39 - WHERE booking__is_instant - ) subq_43 + listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_29.listing = listings_latest_src_28000.listing_id + ) subq_33 + WHERE (listing__is_lux_latest) AND (booking__is_instant) +) + +, cm_12_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(booking_value) AS booking_value + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + is_instant AS booking__is_instant + , booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_39 + WHERE booking__is_instant +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(cm_13_cte.average_booking_value) AS average_booking_value + , MAX(cm_13_cte.bookings) AS bookings + , MAX(cm_12_cte.booking_value) AS booking_value + FROM cm_13_cte cm_13_cte + CROSS JOIN + cm_12_cte cm_12_cte ) subq_44 -) subq_45 +) + +, cm_15_cte AS ( + -- Compute Metrics via Expressions + SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate + FROM ( + -- Read From CTE For node_id=cm_14 + SELECT + instant_lux_booking_value_rate + FROM cm_14_cte cm_14_cte + ) subq_45 +) + +SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate +FROM cm_15_cte cm_15_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_offsets__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_offsets__plan0_optimized.sql index afed1816e..1f249ad76 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_offsets__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_offsets__plan0_optimized.sql @@ -2,44 +2,63 @@ test_name: test_nested_offsets test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_22 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - ON - DATEADD(day, -5, subq_15.ds) = subq_13.metric_time__day - GROUP BY - subq_15.ds - ) subq_19 - ) subq_20 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 ON - DATEADD(day, -2, subq_22.ds) = subq_20.metric_time__day -) subq_23 + DATEADD(day, -5, subq_15.ds) = subq_13.metric_time__day + GROUP BY + subq_15.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_22.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_22 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATEADD(day, -2, subq_22.ds) = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_offsets_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_offsets_with_time_constraint__plan0_optimized.sql index 49e0d1fdd..ba82d841e 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_offsets_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_offsets_with_time_constraint__plan0_optimized.sql @@ -2,50 +2,69 @@ test_name: test_nested_offsets_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.metric_time__day AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ( - -- Time Spine - SELECT - ds AS metric_time__day - FROM ***************************.mf_time_spine subq_22 - WHERE ds BETWEEN '2020-01-12' AND '2020-01-13' - ) subq_21 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 + ON + DATEADD(day, -5, subq_15.ds) = subq_13.metric_time__day + GROUP BY + subq_15.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 SELECT metric_time__day - , 2 * bookings AS bookings_offset_once + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_21.metric_time__day AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Time Spine SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - ON - DATEADD(day, -5, subq_15.ds) = subq_13.metric_time__day - GROUP BY - subq_15.ds - ) subq_19 - ) subq_20 - ON - DATEADD(day, -2, subq_21.metric_time__day) = subq_20.metric_time__day -) subq_23 + ds AS metric_time__day + FROM ***************************.mf_time_spine subq_22 + WHERE ds BETWEEN '2020-01-12' AND '2020-01-13' + ) subq_21 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATEADD(day, -2, subq_21.metric_time__day) = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_offsets_with_where_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_offsets_with_where_constraint__plan0_optimized.sql index ec6b4352f..2e79d6bdd 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_offsets_with_where_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_offsets_with_where_constraint__plan0_optimized.sql @@ -2,51 +2,70 @@ test_name: test_nested_offsets_with_where_constraint test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_16.ds AS metric_time__day + , SUM(subq_14.bookings) AS bookings + FROM ***************************.mf_time_spine subq_16 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_14 + ON + DATEADD(day, -5, subq_16.ds) = subq_14.metric_time__day + GROUP BY + subq_16.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_20 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__day - , bookings_offset_once + , 2 * bookings_offset_once AS bookings_offset_twice FROM ( - -- Join to Time Spine Dataset + -- Constrain Output with WHERE SELECT - subq_23.ds AS metric_time__day - , subq_21.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_16.ds AS metric_time__day - , SUM(subq_14.bookings) AS bookings - FROM ***************************.mf_time_spine subq_16 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_14 - ON - DATEADD(day, -5, subq_16.ds) = subq_14.metric_time__day - GROUP BY - subq_16.ds - ) subq_20 - ) subq_21 - ON - DATEADD(day, -2, subq_23.ds) = subq_21.metric_time__day - ) subq_24 - WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' -) subq_25 + subq_23.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATEADD(day, -2, subq_23.ds) = cm_7_cte.metric_time__day + ) subq_24 + WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' + ) subq_25 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index a45feb035..bf0f4dcbd 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,11 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with one granularity and filtered by a different one. sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__month'] -- Aggregate Measures @@ -38,4 +35,23 @@ FROM ( WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__month -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql index 5b6603fdf..bba6b89a4 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql @@ -4,13 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with multiple granularities. sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day', 'metric_time__month', 'metric_time__year'] -- Aggregate Measures @@ -35,4 +30,29 @@ FROM ( subq_11.ds , DATE_TRUNC('month', subq_11.ds) , DATE_TRUNC('year', subq_11.ds) -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql index c76a35a7a..064ce82fd 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_to_grain_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.booking__ds__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + DATE_TRUNC('month', subq_22.ds) = subq_20.booking__ds__day GROUP BY - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index 0acbd8cac..7346ae28c 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,63 +4,76 @@ docstring: Test a query where an offset window metric is queried with one granularity and filtered by a different one. sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) AS metric_time__month - , MAX(subq_24.booking_value) AS booking_value - , MAX(subq_30.bookers) AS bookers + metric_time__month + , SUM(booking_value) AS booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__month - , SUM(booking_value) AS booking_value - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_19.ds AS metric_time__day - , DATE_TRUNC('month', subq_19.ds) AS metric_time__month - , bookings_source_src_28000.booking_value AS booking_value - FROM ***************************.mf_time_spine subq_19 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 - ON - DATEADD(week, -1, subq_19.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) - ) subq_20 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_24 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookers', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_19.ds AS metric_time__day + , DATE_TRUNC('month', subq_19.ds) AS metric_time__month + , bookings_source_src_28000.booking_value AS booking_value + FROM ***************************.mf_time_spine subq_19 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATEADD(week, -1, subq_19.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + ) subq_20 + WHERE metric_time__day = '2020-01-01' + GROUP BY + metric_time__month +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__month + , COUNT(DISTINCT bookers) AS bookers + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__month - , COUNT(DISTINCT bookers) AS bookers - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , DATE_TRUNC('month', ds) AS metric_time__month - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_30 - ON - subq_24.metric_time__month = subq_30.metric_time__month + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('month', ds) AS metric_time__month + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + WHERE metric_time__day = '2020-01-01' GROUP BY - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) -) subq_31 + metric_time__month +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + GROUP BY + COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) + ) subq_31 +) + +SELECT + metric_time__month AS metric_time__month + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_window_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_window_metric_multiple_granularities__plan0_optimized.sql index 6d98d432d..5a85516ea 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_window_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_window_metric_multiple_granularities__plan0_optimized.sql @@ -4,67 +4,82 @@ docstring: Test a query where an offset window metric is queried with multiple granularities. sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS metric_time__day + , DATE_TRUNC('month', subq_17.ds) AS metric_time__month + , DATE_TRUNC('year', subq_17.ds) AS metric_time__year + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATEADD(week, -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + GROUP BY + subq_17.ds + , DATE_TRUNC('month', subq_17.ds) + , DATE_TRUNC('year', subq_17.ds) +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('month', ds) AS metric_time__month + , DATE_TRUNC('year', ds) AS metric_time__year + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) + , DATE_TRUNC('month', ds) + , DATE_TRUNC('year', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) AS metric_time__month - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) AS metric_time__year - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + metric_time__day + , metric_time__month + , metric_time__year + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS metric_time__day - , DATE_TRUNC('month', subq_17.ds) AS metric_time__month - , DATE_TRUNC('year', subq_17.ds) AS metric_time__year - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - DATEADD(week, -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) AND ( + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + ) AND ( + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + ) GROUP BY - subq_17.ds - , DATE_TRUNC('month', subq_17.ds) - , DATE_TRUNC('year', subq_17.ds) - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , DATE_TRUNC('month', ds) AS metric_time__month - , DATE_TRUNC('year', ds) AS metric_time__year - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - , DATE_TRUNC('month', ds) - , DATE_TRUNC('year', ds) - ) subq_26 - ON - ( - subq_21.metric_time__day = subq_26.metric_time__day - ) AND ( - subq_21.metric_time__month = subq_26.metric_time__month - ) AND ( - subq_21.metric_time__year = subq_26.metric_time__year - ) - GROUP BY - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) -) subq_27 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + , COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) + , COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_window_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_window_with_agg_time_dim__plan0_optimized.sql index 38008ad1e..09dd1173b 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_window_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_window_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATEADD(day, -14, subq_22.ds) = subq_20.booking__ds__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + DATEADD(day, -14, subq_22.ds) = subq_20.booking__ds__day GROUP BY - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_time_offset_metric_with_time_constraint__plan0_optimized.sql index 670d1cb05..fe448492a 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -33,4 +30,23 @@ FROM ( DATEADD(day, -5, subq_10.metric_time__day) = subq_9.metric_time__day GROUP BY subq_10.metric_time__day -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql index 97593fc28..038fe99c2 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_cumulative_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Constrain Time Range to [2019-12-19T00:00:00, 2020-01-02T00:00:00] -- Pass Only Elements: ['bookers', 'metric_time__day'] @@ -42,4 +39,23 @@ FROM ( WHERE subq_17.metric_time__day BETWEEN '2019-12-19' AND '2020-01-02' GROUP BY subq_17.metric_time__day -) subq_23 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric__plan0_optimized.sql index d5922afa2..21ba47a49 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -25,4 +22,24 @@ FROM ( ) subq_13 GROUP BY metric_time__day -) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , ref_bookings + , bookings + FROM cm_8_cte cm_8_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , non_referred_bookings_pct AS non_referred_bookings_pct +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql index 46c9cc3be..2a3c63365 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql @@ -6,60 +6,81 @@ sql_engine: Postgres --- -- Combine Aggregated Outputs -- Order By [] Limit 1 -SELECT - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) AS booking__is_instant - , MAX(subq_18.derived_shared_alias_1a) AS derived_shared_alias_1a - , MAX(subq_24.derived_shared_alias_2) AS derived_shared_alias_2 -FROM ( +WITH cm_8_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + GROUP BY + booking__is_instant +) + +, cm_9_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias - 10 AS derived_shared_alias_1a FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_8 SELECT booking__is_instant - , SUM(bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_8_cte cm_8_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_10_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(instant_bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_21 + GROUP BY + booking__is_instant +) + +, cm_11_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias + 10 AS derived_shared_alias_2 FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_10 SELECT booking__is_instant - , SUM(instant_bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_21 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_10_cte cm_10_cte ) subq_23 -) subq_24 +) + +SELECT + COALESCE(cm_9_cte.booking__is_instant, cm_11_cte.booking__is_instant) AS booking__is_instant + , MAX(cm_9_cte.derived_shared_alias_1a) AS derived_shared_alias_1a + , MAX(cm_11_cte.derived_shared_alias_2) AS derived_shared_alias_2 +FROM cm_9_cte cm_9_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.booking__is_instant = subq_24.booking__is_instant + cm_9_cte.booking__is_instant = cm_11_cte.booking__is_instant GROUP BY - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) + COALESCE(cm_9_cte.booking__is_instant, cm_11_cte.booking__is_instant) LIMIT 1 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql index 0a4583625..d6fd2f701 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_month_dimension_and_offset_window test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_last_month AS bookings_last_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] -- Aggregate Measures @@ -28,4 +25,23 @@ FROM ( subq_10.metric_time__month - MAKE_INTERVAL(months => 1) = DATE_TRUNC('month', monthly_bookings_source_src_16000.ds) GROUP BY subq_10.metric_time__month -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_last_month AS bookings_last_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_last_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_last_month AS bookings_last_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_to_grain__plan0_optimized.sql index ece4b86e4..17fd879ca 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_to_grain__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql index 32cb49a8e..fe17b6f08 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,59 +2,72 @@ test_name: test_derived_metric_with_offset_to_grain_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) AS metric_time__week - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__week + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__week - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__week'] - SELECT - DATE_TRUNC('week', ds) AS metric_time__week - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__week - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__week'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATE_TRUNC('week', subq_22.ds) AS metric_time__week - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day - WHERE DATE_TRUNC('week', subq_22.ds) = subq_22.ds - GROUP BY - DATE_TRUNC('week', subq_22.ds) - ) subq_26 + DATE_TRUNC('week', ds) AS metric_time__week + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__week +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__week'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('week', subq_22.ds) AS metric_time__week + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__week = subq_26.metric_time__week + DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day + WHERE DATE_TRUNC('week', subq_22.ds) = subq_22.ds GROUP BY - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) -) subq_27 + DATE_TRUNC('week', subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__week, cm_7_cte.metric_time__week) AS metric_time__week + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__week = cm_7_cte.metric_time__week + GROUP BY + COALESCE(cm_6_cte.metric_time__week, cm_7_cte.metric_time__week) + ) subq_27 +) + +SELECT + metric_time__week AS metric_time__week + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window__plan0_optimized.sql index 9e88eaf12..e1f2a2ebe 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - subq_22.ds - MAKE_INTERVAL(days => 14) = subq_20.metric_time__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + subq_22.ds - MAKE_INTERVAL(days => 14) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql index 53a9fe09c..2fdf14c91 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__quarter - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) AS metric_time__quarter - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__quarter + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__quarter - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - SELECT - DATE_TRUNC('quarter', ds) AS metric_time__quarter - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__quarter - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATE_TRUNC('quarter', subq_22.ds) AS metric_time__quarter - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - subq_22.ds - MAKE_INTERVAL(days => 14) = subq_20.metric_time__day - GROUP BY - DATE_TRUNC('quarter', subq_22.ds) - ) subq_26 + DATE_TRUNC('quarter', ds) AS metric_time__quarter + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__quarter +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__quarter'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('quarter', subq_22.ds) AS metric_time__quarter + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__quarter = subq_26.metric_time__quarter + subq_22.ds - MAKE_INTERVAL(days => 14) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) -) subq_27 + DATE_TRUNC('quarter', subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__quarter + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__quarter, cm_7_cte.metric_time__quarter) AS metric_time__quarter + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__quarter = cm_7_cte.metric_time__quarter + GROUP BY + COALESCE(cm_6_cte.metric_time__quarter, cm_7_cte.metric_time__quarter) + ) subq_27 +) + +SELECT + metric_time__quarter AS metric_time__quarter + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql index 97deaf2d8..6f177f322 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql @@ -2,62 +2,75 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_20.ds AS metric_time__day + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds AS metric_time__day - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day - GROUP BY - subq_20.ds - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day + GROUP BY + subq_20.ds +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - subq_28.ds - MAKE_INTERVAL(months => 1) = subq_26.metric_time__day - GROUP BY - subq_28.ds - ) subq_32 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__day = subq_32.metric_time__day + subq_28.ds - MAKE_INTERVAL(months => 1) = subq_26.metric_time__day GROUP BY - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) -) subq_33 + subq_28.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql index d5a61f7a8..102817762 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,63 +2,76 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain_and_granul test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__year - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) AS metric_time__year - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('year', subq_20.ds) AS metric_time__year + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATE_TRUNC('year', subq_20.ds) AS metric_time__year - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day - WHERE DATE_TRUNC('year', subq_20.ds) = subq_20.ds - GROUP BY - DATE_TRUNC('year', subq_20.ds) - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day + WHERE DATE_TRUNC('year', subq_20.ds) = subq_20.ds + GROUP BY + DATE_TRUNC('year', subq_20.ds) +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('year', subq_28.ds) AS metric_time__year + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATE_TRUNC('year', subq_28.ds) AS metric_time__year - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - subq_28.ds - MAKE_INTERVAL(months => 1) = subq_26.metric_time__day - GROUP BY - DATE_TRUNC('year', subq_28.ds) - ) subq_32 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__year = subq_32.metric_time__year + subq_28.ds - MAKE_INTERVAL(months => 1) = subq_26.metric_time__day GROUP BY - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) -) subq_33 + DATE_TRUNC('year', subq_28.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__year + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + GROUP BY + COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) + ) subq_33 +) + +SELECT + metric_time__year AS metric_time__year + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql index 2f0e94a1b..1c8bb796d 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_derived_metric_with_offset_window_and_time_filter test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_17 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_21.bookings) AS bookings - , MAX(subq_30.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings_2_weeks_ago FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( + subq_25.ds AS metric_time__day + , subq_23.bookings AS bookings + FROM ***************************.mf_time_spine subq_25 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT DATE_TRUNC('day', ds) AS metric_time__day , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_17 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' - GROUP BY - metric_time__day - ) subq_21 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + ) subq_23 + ON + subq_25.ds - MAKE_INTERVAL(days => 14) = subq_23.metric_time__day + ) subq_26 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_25.ds AS metric_time__day - , subq_23.bookings AS bookings - FROM ***************************.mf_time_spine subq_25 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - ON - subq_25.ds - MAKE_INTERVAL(days => 14) = subq_23.metric_time__day - ) subq_26 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day GROUP BY - metric_time__day - ) subq_30 - ON - subq_21.metric_time__day = subq_30.metric_time__day - GROUP BY - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) -) subq_31 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_offset_cumulative_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_offset_cumulative_metric__plan0_optimized.sql index 1329a0ade..fdfd1fbf7 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_offset_cumulative_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_offset_cumulative_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_cumulative_metric test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookers', 'metric_time__day'] -- Aggregate Measures @@ -34,4 +31,23 @@ FROM ( subq_17.ds - MAKE_INTERVAL(days => 2) = subq_15.metric_time__day GROUP BY subq_17.ds -) subq_21 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_21 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql index 2f6371d3f..870542635 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql @@ -2,47 +2,60 @@ test_name: test_derived_offset_metric_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS booking__ds__day + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + subq_17.ds - MAKE_INTERVAL(weeks => 1) = DATE_TRUNC('day', bookings_source_src_28000.ds) + GROUP BY + subq_17.ds +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + booking__ds__day + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS booking__ds__day - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - subq_17.ds - MAKE_INTERVAL(weeks => 1) = DATE_TRUNC('day', bookings_source_src_28000.ds) + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day GROUP BY - subq_17.ds - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_26 - ON - subq_21.booking__ds__day = subq_26.booking__ds__day - GROUP BY - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql index 814f94c4a..80402d2c3 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_metric_with_one_input_metric test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( subq_11.ds - MAKE_INTERVAL(days => 5) = subq_9.metric_time__day GROUP BY subq_11.ds -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_multi_metric_fill_null__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_multi_metric_fill_null__plan0_optimized.sql index 49134d7ee..5472cc140 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_multi_metric_fill_null__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_multi_metric_fill_null__plan0_optimized.sql @@ -3,40 +3,45 @@ test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_16.metric_time__day, subq_21.metric_time__day) AS metric_time__day - , MAX(subq_16.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine - , MAX(subq_21.listings) AS listings -FROM ( +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - GROUP BY - metric_time__day - ) subq_14 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 + GROUP BY + metric_time__day + ) subq_14 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_15 -) subq_16 -FULL OUTER JOIN ( +) + +, cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -53,8 +58,16 @@ FULL OUTER JOIN ( ) subq_19 GROUP BY metric_time__day -) subq_21 +) + +SELECT + COALESCE(cm_7_cte.metric_time__day, cm_8_cte.metric_time__day) AS metric_time__day + , MAX(cm_7_cte.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine + , MAX(cm_8_cte.listings) AS listings +FROM cm_7_cte cm_7_cte +FULL OUTER JOIN + cm_8_cte cm_8_cte ON - subq_16.metric_time__day = subq_21.metric_time__day + cm_7_cte.metric_time__day = cm_8_cte.metric_time__day GROUP BY - COALESCE(subq_16.metric_time__day, subq_21.metric_time__day) + COALESCE(cm_7_cte.metric_time__day, cm_8_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_derived_metric__plan0_optimized.sql index a34abf65e..7e0844f02 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_derived_metric__plan0_optimized.sql @@ -2,65 +2,87 @@ test_name: test_nested_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_17 +WITH cm_12_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_28.metric_time__day, subq_33.metric_time__day) AS metric_time__day - , MAX(subq_28.non_referred) AS non_referred - , MAX(subq_33.instant) AS instant - , MAX(subq_33.bookings) AS bookings + metric_time__day + , SUM(referred_bookings) AS ref_bookings + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred - FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(referred_bookings) AS ref_bookings - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - GROUP BY - metric_time__day - ) subq_27 - ) subq_28 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + GROUP BY + metric_time__day +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , SUM(instant_bookings) AS instant - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - GROUP BY - metric_time__day - ) subq_33 - ON - subq_28.metric_time__day = subq_33.metric_time__day + , ref_bookings + , bookings + FROM cm_12_cte cm_12_cte + ) subq_27 +) + +, cm_16_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(instant_bookings) AS instant + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 GROUP BY - COALESCE(subq_28.metric_time__day, subq_33.metric_time__day) -) subq_34 + metric_time__day +) + +, cm_17_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_13_cte.metric_time__day, cm_16_cte.metric_time__day) AS metric_time__day + , MAX(cm_13_cte.non_referred) AS non_referred + , MAX(cm_16_cte.instant) AS instant + , MAX(cm_16_cte.bookings) AS bookings + FROM cm_13_cte cm_13_cte + FULL OUTER JOIN + cm_16_cte cm_16_cte + ON + cm_13_cte.metric_time__day = cm_16_cte.metric_time__day + GROUP BY + COALESCE(cm_13_cte.metric_time__day, cm_16_cte.metric_time__day) + ) subq_34 +) + +SELECT + metric_time__day AS metric_time__day + , instant_plus_non_referred_bookings_pct AS instant_plus_non_referred_bookings_pct +FROM cm_17_cte cm_17_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql index c11fef9c7..6ad0683da 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql @@ -2,57 +2,77 @@ test_name: test_nested_derived_metric_offset_with_joined_where_constraint_not_se test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS metric_time__day + , subq_15.booking__is_instant AS booking__is_instant + , SUM(subq_15.bookings) AS bookings + FROM ***************************.mf_time_spine subq_17 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + ON + subq_17.ds - MAKE_INTERVAL(days => 5) = subq_15.metric_time__day + GROUP BY + subq_17.ds + , subq_15.booking__is_instant +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking__is_instant + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , booking__is_instant + , bookings + FROM cm_6_cte cm_6_cte + ) subq_21 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__day - , bookings_offset_once + , 2 * bookings_offset_once AS bookings_offset_twice FROM ( - -- Join to Time Spine Dataset + -- Constrain Output with WHERE + -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] SELECT - subq_24.ds AS metric_time__day - , subq_22.booking__is_instant AS booking__is_instant - , subq_22.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_24 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , booking__is_instant - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_17.ds AS metric_time__day - , subq_15.booking__is_instant AS booking__is_instant - , SUM(subq_15.bookings) AS bookings - FROM ***************************.mf_time_spine subq_17 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - ON - subq_17.ds - MAKE_INTERVAL(days => 5) = subq_15.metric_time__day - GROUP BY - subq_17.ds - , subq_15.booking__is_instant - ) subq_21 - ) subq_22 - ON - subq_24.ds - MAKE_INTERVAL(days => 2) = subq_22.metric_time__day - ) subq_25 - WHERE booking__is_instant -) subq_27 + subq_24.ds AS metric_time__day + , cm_7_cte.booking__is_instant AS booking__is_instant + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_24 + INNER JOIN + cm_7_cte cm_7_cte + ON + subq_24.ds - MAKE_INTERVAL(days => 2) = cm_7_cte.metric_time__day + ) subq_25 + WHERE booking__is_instant + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql index 2f8d1a2c5..c5fb7d9c2 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql @@ -2,65 +2,95 @@ test_name: test_nested_derived_metric_with_offset_multiple_input_metrics test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_14 +WITH cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_11_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month - , MAX(subq_30.booking_fees) AS booking_fees + metric_time__day + , booking_value * 0.05 AS booking_fees_start_of_month FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_10 SELECT - subq_23.ds AS metric_time__day - , subq_21.booking_fees_start_of_month AS booking_fees_start_of_month - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions - SELECT - metric_time__day - , booking_value * 0.05 AS booking_fees_start_of_month - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_20 - ) subq_21 - ON - DATE_TRUNC('month', subq_23.ds) = subq_21.metric_time__day - ) subq_24 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , booking_value + FROM cm_10_cte cm_10_cte + ) subq_20 +) + +, cm_12_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_value * 0.05 AS booking_fees + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , booking_value * 0.05 AS booking_fees + , booking_value + FROM cm_12_cte cm_12_cte + ) subq_29 +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_24.metric_time__day, cm_13_cte.metric_time__day) AS metric_time__day + , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month + , MAX(cm_13_cte.booking_fees) AS booking_fees FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_29 - ) subq_30 - ON - subq_24.metric_time__day = subq_30.metric_time__day - GROUP BY - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) -) subq_31 + subq_23.ds AS metric_time__day + , cm_11_cte.booking_fees_start_of_month AS booking_fees_start_of_month + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_11_cte cm_11_cte + ON + DATE_TRUNC('month', subq_23.ds) = cm_11_cte.metric_time__day + ) subq_24 + FULL OUTER JOIN + cm_13_cte cm_13_cte + ON + subq_24.metric_time__day = cm_13_cte.metric_time__day + GROUP BY + COALESCE(subq_24.metric_time__day, cm_13_cte.metric_time__day) + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , booking_fees_since_start_of_month AS booking_fees_since_start_of_month +FROM cm_14_cte cm_14_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql index 1811e2491..9aeff14b7 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql @@ -2,36 +2,60 @@ test_name: test_nested_fill_nulls_without_time_spine test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_8 - GROUP BY - metric_time__day - ) subq_9 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_8 + GROUP BY + metric_time__day + ) subq_9 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_10 -) subq_11 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_7_cte cm_7_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , nested_fill_nulls_without_time_spine AS nested_fill_nulls_without_time_spine +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql index 78486ed3b..825f2815e 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql @@ -3,46 +3,59 @@ test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_18.metric_time__day, subq_23.metric_time__day) AS metric_time__day - , MAX(subq_18.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_8_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine - FROM ( - -- Aggregate Measures - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_14 - GROUP BY - metric_time__day - ) subq_15 - ) subq_16 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_14 + GROUP BY + metric_time__day + ) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_8_cte cm_8_cte + ) subq_16 +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_9 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_9_cte cm_9_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_11_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -59,8 +72,16 @@ FULL OUTER JOIN ( ) subq_21 GROUP BY metric_time__day -) subq_23 +) + +SELECT + COALESCE(cm_10_cte.metric_time__day, cm_11_cte.metric_time__day) AS metric_time__day + , MAX(cm_10_cte.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine + , MAX(cm_11_cte.listings) AS listings +FROM cm_10_cte cm_10_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.metric_time__day = subq_23.metric_time__day + cm_10_cte.metric_time__day = cm_11_cte.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_23.metric_time__day) + COALESCE(cm_10_cte.metric_time__day, cm_11_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_filters__plan0_optimized.sql index 1f99c4eda..3c7e2f885 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_filters__plan0_optimized.sql @@ -4,67 +4,86 @@ docstring: Tests derived metric rendering for a nested derived metric with filters on the outer metric spec. sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - instant_lux_booking_value_rate AS instant_lux_booking_value_rate -FROM ( +-- Read From CTE For node_id=cm_15 +WITH cm_13_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings'] + -- Aggregate Measures -- Compute Metrics via Expressions SELECT - average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Combine Aggregated Outputs + -- Join Standard Outputs SELECT - MAX(subq_37.average_booking_value) AS average_booking_value - , MAX(subq_37.bookings) AS bookings - , MAX(subq_43.booking_value) AS booking_value + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_29.booking__is_instant AS booking__is_instant + , subq_29.bookings AS bookings + , subq_29.average_booking_value AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_29.booking__is_instant AS booking__is_instant - , subq_29.bookings AS bookings - , subq_29.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_29.listing = listings_latest_src_28000.listing_id - ) subq_33 - WHERE (listing__is_lux_latest) AND (booking__is_instant) - ) subq_37 - CROSS JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value',] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - SUM(booking_value) AS booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_39 - WHERE booking__is_instant - ) subq_43 + listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_29.listing = listings_latest_src_28000.listing_id + ) subq_33 + WHERE (listing__is_lux_latest) AND (booking__is_instant) +) + +, cm_12_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(booking_value) AS booking_value + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + is_instant AS booking__is_instant + , booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_39 + WHERE booking__is_instant +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(cm_13_cte.average_booking_value) AS average_booking_value + , MAX(cm_13_cte.bookings) AS bookings + , MAX(cm_12_cte.booking_value) AS booking_value + FROM cm_13_cte cm_13_cte + CROSS JOIN + cm_12_cte cm_12_cte ) subq_44 -) subq_45 +) + +, cm_15_cte AS ( + -- Compute Metrics via Expressions + SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate + FROM ( + -- Read From CTE For node_id=cm_14 + SELECT + instant_lux_booking_value_rate + FROM cm_14_cte cm_14_cte + ) subq_45 +) + +SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate +FROM cm_15_cte cm_15_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_offsets__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_offsets__plan0_optimized.sql index ba241866d..0e33a44f5 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_offsets__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_offsets__plan0_optimized.sql @@ -2,44 +2,63 @@ test_name: test_nested_offsets test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_22 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - ON - subq_15.ds - MAKE_INTERVAL(days => 5) = subq_13.metric_time__day - GROUP BY - subq_15.ds - ) subq_19 - ) subq_20 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 ON - subq_22.ds - MAKE_INTERVAL(days => 2) = subq_20.metric_time__day -) subq_23 + subq_15.ds - MAKE_INTERVAL(days => 5) = subq_13.metric_time__day + GROUP BY + subq_15.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_22.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_22 + INNER JOIN + cm_7_cte cm_7_cte + ON + subq_22.ds - MAKE_INTERVAL(days => 2) = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_offsets_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_offsets_with_time_constraint__plan0_optimized.sql index 812192844..20500ef9f 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_offsets_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_offsets_with_time_constraint__plan0_optimized.sql @@ -2,50 +2,69 @@ test_name: test_nested_offsets_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.metric_time__day AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ( - -- Time Spine - SELECT - ds AS metric_time__day - FROM ***************************.mf_time_spine subq_22 - WHERE ds BETWEEN '2020-01-12' AND '2020-01-13' - ) subq_21 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 + ON + subq_15.ds - MAKE_INTERVAL(days => 5) = subq_13.metric_time__day + GROUP BY + subq_15.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 SELECT metric_time__day - , 2 * bookings AS bookings_offset_once + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_21.metric_time__day AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Time Spine SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - ON - subq_15.ds - MAKE_INTERVAL(days => 5) = subq_13.metric_time__day - GROUP BY - subq_15.ds - ) subq_19 - ) subq_20 - ON - subq_21.metric_time__day - MAKE_INTERVAL(days => 2) = subq_20.metric_time__day -) subq_23 + ds AS metric_time__day + FROM ***************************.mf_time_spine subq_22 + WHERE ds BETWEEN '2020-01-12' AND '2020-01-13' + ) subq_21 + INNER JOIN + cm_7_cte cm_7_cte + ON + subq_21.metric_time__day - MAKE_INTERVAL(days => 2) = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_offsets_with_where_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_offsets_with_where_constraint__plan0_optimized.sql index 0aa067052..0112c4e04 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_offsets_with_where_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_offsets_with_where_constraint__plan0_optimized.sql @@ -2,51 +2,70 @@ test_name: test_nested_offsets_with_where_constraint test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_16.ds AS metric_time__day + , SUM(subq_14.bookings) AS bookings + FROM ***************************.mf_time_spine subq_16 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_14 + ON + subq_16.ds - MAKE_INTERVAL(days => 5) = subq_14.metric_time__day + GROUP BY + subq_16.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_20 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__day - , bookings_offset_once + , 2 * bookings_offset_once AS bookings_offset_twice FROM ( - -- Join to Time Spine Dataset + -- Constrain Output with WHERE SELECT - subq_23.ds AS metric_time__day - , subq_21.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_16.ds AS metric_time__day - , SUM(subq_14.bookings) AS bookings - FROM ***************************.mf_time_spine subq_16 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_14 - ON - subq_16.ds - MAKE_INTERVAL(days => 5) = subq_14.metric_time__day - GROUP BY - subq_16.ds - ) subq_20 - ) subq_21 - ON - subq_23.ds - MAKE_INTERVAL(days => 2) = subq_21.metric_time__day - ) subq_24 - WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' -) subq_25 + subq_23.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_7_cte cm_7_cte + ON + subq_23.ds - MAKE_INTERVAL(days => 2) = cm_7_cte.metric_time__day + ) subq_24 + WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' + ) subq_25 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index 52f737090..a53c0c5bc 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,11 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with one granularity and filtered by a different one. sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__month'] -- Aggregate Measures @@ -38,4 +35,23 @@ FROM ( WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__month -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql index 321cb4ac6..a8b9acee1 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql @@ -4,13 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with multiple granularities. sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day', 'metric_time__month', 'metric_time__year'] -- Aggregate Measures @@ -35,4 +30,29 @@ FROM ( subq_11.ds , DATE_TRUNC('month', subq_11.ds) , DATE_TRUNC('year', subq_11.ds) -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql index e102ca870..31bab5f6f 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_to_grain_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.booking__ds__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + DATE_TRUNC('month', subq_22.ds) = subq_20.booking__ds__day GROUP BY - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index 3934f7c7b..4ac58c701 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,63 +4,76 @@ docstring: Test a query where an offset window metric is queried with one granularity and filtered by a different one. sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) AS metric_time__month - , MAX(subq_24.booking_value) AS booking_value - , MAX(subq_30.bookers) AS bookers + metric_time__month + , SUM(booking_value) AS booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__month - , SUM(booking_value) AS booking_value - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_19.ds AS metric_time__day - , DATE_TRUNC('month', subq_19.ds) AS metric_time__month - , bookings_source_src_28000.booking_value AS booking_value - FROM ***************************.mf_time_spine subq_19 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 - ON - subq_19.ds - MAKE_INTERVAL(weeks => 1) = DATE_TRUNC('day', bookings_source_src_28000.ds) - ) subq_20 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_24 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookers', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_19.ds AS metric_time__day + , DATE_TRUNC('month', subq_19.ds) AS metric_time__month + , bookings_source_src_28000.booking_value AS booking_value + FROM ***************************.mf_time_spine subq_19 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + subq_19.ds - MAKE_INTERVAL(weeks => 1) = DATE_TRUNC('day', bookings_source_src_28000.ds) + ) subq_20 + WHERE metric_time__day = '2020-01-01' + GROUP BY + metric_time__month +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__month + , COUNT(DISTINCT bookers) AS bookers + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__month - , COUNT(DISTINCT bookers) AS bookers - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , DATE_TRUNC('month', ds) AS metric_time__month - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_30 - ON - subq_24.metric_time__month = subq_30.metric_time__month + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('month', ds) AS metric_time__month + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + WHERE metric_time__day = '2020-01-01' GROUP BY - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) -) subq_31 + metric_time__month +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + GROUP BY + COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) + ) subq_31 +) + +SELECT + metric_time__month AS metric_time__month + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_window_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_window_metric_multiple_granularities__plan0_optimized.sql index 34c12c7bd..1b7a0b533 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_window_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_window_metric_multiple_granularities__plan0_optimized.sql @@ -4,67 +4,82 @@ docstring: Test a query where an offset window metric is queried with multiple granularities. sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS metric_time__day + , DATE_TRUNC('month', subq_17.ds) AS metric_time__month + , DATE_TRUNC('year', subq_17.ds) AS metric_time__year + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + subq_17.ds - MAKE_INTERVAL(weeks => 1) = DATE_TRUNC('day', bookings_source_src_28000.ds) + GROUP BY + subq_17.ds + , DATE_TRUNC('month', subq_17.ds) + , DATE_TRUNC('year', subq_17.ds) +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('month', ds) AS metric_time__month + , DATE_TRUNC('year', ds) AS metric_time__year + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) + , DATE_TRUNC('month', ds) + , DATE_TRUNC('year', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) AS metric_time__month - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) AS metric_time__year - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + metric_time__day + , metric_time__month + , metric_time__year + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS metric_time__day - , DATE_TRUNC('month', subq_17.ds) AS metric_time__month - , DATE_TRUNC('year', subq_17.ds) AS metric_time__year - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - subq_17.ds - MAKE_INTERVAL(weeks => 1) = DATE_TRUNC('day', bookings_source_src_28000.ds) + ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) AND ( + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + ) AND ( + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + ) GROUP BY - subq_17.ds - , DATE_TRUNC('month', subq_17.ds) - , DATE_TRUNC('year', subq_17.ds) - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , DATE_TRUNC('month', ds) AS metric_time__month - , DATE_TRUNC('year', ds) AS metric_time__year - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - , DATE_TRUNC('month', ds) - , DATE_TRUNC('year', ds) - ) subq_26 - ON - ( - subq_21.metric_time__day = subq_26.metric_time__day - ) AND ( - subq_21.metric_time__month = subq_26.metric_time__month - ) AND ( - subq_21.metric_time__year = subq_26.metric_time__year - ) - GROUP BY - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) -) subq_27 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + , COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) + , COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_window_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_window_with_agg_time_dim__plan0_optimized.sql index 9d3549dd5..2971e204e 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_window_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_window_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - subq_22.ds - MAKE_INTERVAL(days => 14) = subq_20.booking__ds__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + subq_22.ds - MAKE_INTERVAL(days => 14) = subq_20.booking__ds__day GROUP BY - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_time_offset_metric_with_time_constraint__plan0_optimized.sql index 904c87a36..a7b8633cc 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -33,4 +30,23 @@ FROM ( subq_10.metric_time__day - MAKE_INTERVAL(days => 5) = subq_9.metric_time__day GROUP BY subq_10.metric_time__day -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql index ac52eb2c8..639f902e4 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_cumulative_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Constrain Time Range to [2019-12-19T00:00:00, 2020-01-02T00:00:00] -- Pass Only Elements: ['bookers', 'metric_time__day'] @@ -42,4 +39,23 @@ FROM ( WHERE subq_17.metric_time__day BETWEEN '2019-12-19' AND '2020-01-02' GROUP BY subq_17.metric_time__day -) subq_23 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric__plan0_optimized.sql index 49941151d..cd8547539 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -25,4 +22,24 @@ FROM ( ) subq_13 GROUP BY metric_time__day -) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , ref_bookings + , bookings + FROM cm_8_cte cm_8_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , non_referred_bookings_pct AS non_referred_bookings_pct +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql index 71f376db7..755c05e5e 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql @@ -6,60 +6,81 @@ sql_engine: Redshift --- -- Combine Aggregated Outputs -- Order By [] Limit 1 -SELECT - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) AS booking__is_instant - , MAX(subq_18.derived_shared_alias_1a) AS derived_shared_alias_1a - , MAX(subq_24.derived_shared_alias_2) AS derived_shared_alias_2 -FROM ( +WITH cm_8_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + GROUP BY + booking__is_instant +) + +, cm_9_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias - 10 AS derived_shared_alias_1a FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_8 SELECT booking__is_instant - , SUM(bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_8_cte cm_8_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_10_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(instant_bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_21 + GROUP BY + booking__is_instant +) + +, cm_11_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias + 10 AS derived_shared_alias_2 FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_10 SELECT booking__is_instant - , SUM(instant_bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_21 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_10_cte cm_10_cte ) subq_23 -) subq_24 +) + +SELECT + COALESCE(cm_9_cte.booking__is_instant, cm_11_cte.booking__is_instant) AS booking__is_instant + , MAX(cm_9_cte.derived_shared_alias_1a) AS derived_shared_alias_1a + , MAX(cm_11_cte.derived_shared_alias_2) AS derived_shared_alias_2 +FROM cm_9_cte cm_9_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.booking__is_instant = subq_24.booking__is_instant + cm_9_cte.booking__is_instant = cm_11_cte.booking__is_instant GROUP BY - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) + COALESCE(cm_9_cte.booking__is_instant, cm_11_cte.booking__is_instant) LIMIT 1 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql index d755bb7d5..42871e8a0 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_month_dimension_and_offset_window test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_last_month AS bookings_last_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] -- Aggregate Measures @@ -28,4 +25,23 @@ FROM ( DATEADD(month, -1, subq_10.metric_time__month) = DATE_TRUNC('month', monthly_bookings_source_src_16000.ds) GROUP BY subq_10.metric_time__month -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_last_month AS bookings_last_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_last_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_last_month AS bookings_last_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_to_grain__plan0_optimized.sql index 464cffe60..e14d68703 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_to_grain__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql index d9147b7bd..1211ddea4 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,59 +2,72 @@ test_name: test_derived_metric_with_offset_to_grain_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) AS metric_time__week - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__week + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__week - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__week'] - SELECT - DATE_TRUNC('week', ds) AS metric_time__week - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__week - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__week'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATE_TRUNC('week', subq_22.ds) AS metric_time__week - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day - WHERE DATE_TRUNC('week', subq_22.ds) = subq_22.ds - GROUP BY - DATE_TRUNC('week', subq_22.ds) - ) subq_26 + DATE_TRUNC('week', ds) AS metric_time__week + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__week +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__week'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('week', subq_22.ds) AS metric_time__week + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__week = subq_26.metric_time__week + DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day + WHERE DATE_TRUNC('week', subq_22.ds) = subq_22.ds GROUP BY - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) -) subq_27 + DATE_TRUNC('week', subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__week, cm_7_cte.metric_time__week) AS metric_time__week + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__week = cm_7_cte.metric_time__week + GROUP BY + COALESCE(cm_6_cte.metric_time__week, cm_7_cte.metric_time__week) + ) subq_27 +) + +SELECT + metric_time__week AS metric_time__week + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window__plan0_optimized.sql index cfa62f92a..d0b975f19 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql index b6bc0d71d..301c8b681 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__quarter - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) AS metric_time__quarter - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__quarter + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__quarter - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - SELECT - DATE_TRUNC('quarter', ds) AS metric_time__quarter - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__quarter - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATE_TRUNC('quarter', subq_22.ds) AS metric_time__quarter - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day - GROUP BY - DATE_TRUNC('quarter', subq_22.ds) - ) subq_26 + DATE_TRUNC('quarter', ds) AS metric_time__quarter + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__quarter +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__quarter'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('quarter', subq_22.ds) AS metric_time__quarter + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__quarter = subq_26.metric_time__quarter + DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) -) subq_27 + DATE_TRUNC('quarter', subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__quarter + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__quarter, cm_7_cte.metric_time__quarter) AS metric_time__quarter + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__quarter = cm_7_cte.metric_time__quarter + GROUP BY + COALESCE(cm_6_cte.metric_time__quarter, cm_7_cte.metric_time__quarter) + ) subq_27 +) + +SELECT + metric_time__quarter AS metric_time__quarter + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql index f23d4f512..a2109348a 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql @@ -2,62 +2,75 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_20.ds AS metric_time__day + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds AS metric_time__day - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day - GROUP BY - subq_20.ds - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day + GROUP BY + subq_20.ds +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - DATEADD(month, -1, subq_28.ds) = subq_26.metric_time__day - GROUP BY - subq_28.ds - ) subq_32 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__day = subq_32.metric_time__day + DATEADD(month, -1, subq_28.ds) = subq_26.metric_time__day GROUP BY - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) -) subq_33 + subq_28.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql index c0e2a731e..db6b2a9db 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,63 +2,76 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain_and_granul test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__year - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) AS metric_time__year - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('year', subq_20.ds) AS metric_time__year + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATE_TRUNC('year', subq_20.ds) AS metric_time__year - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day - WHERE DATE_TRUNC('year', subq_20.ds) = subq_20.ds - GROUP BY - DATE_TRUNC('year', subq_20.ds) - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day + WHERE DATE_TRUNC('year', subq_20.ds) = subq_20.ds + GROUP BY + DATE_TRUNC('year', subq_20.ds) +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('year', subq_28.ds) AS metric_time__year + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATE_TRUNC('year', subq_28.ds) AS metric_time__year - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - DATEADD(month, -1, subq_28.ds) = subq_26.metric_time__day - GROUP BY - DATE_TRUNC('year', subq_28.ds) - ) subq_32 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__year = subq_32.metric_time__year + DATEADD(month, -1, subq_28.ds) = subq_26.metric_time__day GROUP BY - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) -) subq_33 + DATE_TRUNC('year', subq_28.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__year + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + GROUP BY + COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) + ) subq_33 +) + +SELECT + metric_time__year AS metric_time__year + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql index 398662084..d25592a92 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_derived_metric_with_offset_window_and_time_filter test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_17 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_21.bookings) AS bookings - , MAX(subq_30.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings_2_weeks_ago FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( + subq_25.ds AS metric_time__day + , subq_23.bookings AS bookings + FROM ***************************.mf_time_spine subq_25 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT DATE_TRUNC('day', ds) AS metric_time__day , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_17 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' - GROUP BY - metric_time__day - ) subq_21 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + ) subq_23 + ON + DATEADD(day, -14, subq_25.ds) = subq_23.metric_time__day + ) subq_26 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_25.ds AS metric_time__day - , subq_23.bookings AS bookings - FROM ***************************.mf_time_spine subq_25 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - ON - DATEADD(day, -14, subq_25.ds) = subq_23.metric_time__day - ) subq_26 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day GROUP BY - metric_time__day - ) subq_30 - ON - subq_21.metric_time__day = subq_30.metric_time__day - GROUP BY - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) -) subq_31 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_offset_cumulative_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_offset_cumulative_metric__plan0_optimized.sql index 28071a31e..399cdd798 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_offset_cumulative_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_offset_cumulative_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_cumulative_metric test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookers', 'metric_time__day'] -- Aggregate Measures @@ -34,4 +31,23 @@ FROM ( DATEADD(day, -2, subq_17.ds) = subq_15.metric_time__day GROUP BY subq_17.ds -) subq_21 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_21 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql index 67fe080a8..d6fcd75ea 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql @@ -2,47 +2,60 @@ test_name: test_derived_offset_metric_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS booking__ds__day + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATEADD(week, -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + GROUP BY + subq_17.ds +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + booking__ds__day + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS booking__ds__day - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - DATEADD(week, -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day GROUP BY - subq_17.ds - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_26 - ON - subq_21.booking__ds__day = subq_26.booking__ds__day - GROUP BY - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql index 6b9a08d0b..2b25b163a 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_metric_with_one_input_metric test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATEADD(day, -5, subq_11.ds) = subq_9.metric_time__day GROUP BY subq_11.ds -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_multi_metric_fill_null__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_multi_metric_fill_null__plan0_optimized.sql index 3fcf40b4d..7e26643ba 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_multi_metric_fill_null__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_multi_metric_fill_null__plan0_optimized.sql @@ -3,40 +3,45 @@ test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_16.metric_time__day, subq_21.metric_time__day) AS metric_time__day - , MAX(subq_16.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine - , MAX(subq_21.listings) AS listings -FROM ( +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - GROUP BY - metric_time__day - ) subq_14 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 + GROUP BY + metric_time__day + ) subq_14 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_15 -) subq_16 -FULL OUTER JOIN ( +) + +, cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -53,8 +58,16 @@ FULL OUTER JOIN ( ) subq_19 GROUP BY metric_time__day -) subq_21 +) + +SELECT + COALESCE(cm_7_cte.metric_time__day, cm_8_cte.metric_time__day) AS metric_time__day + , MAX(cm_7_cte.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine + , MAX(cm_8_cte.listings) AS listings +FROM cm_7_cte cm_7_cte +FULL OUTER JOIN + cm_8_cte cm_8_cte ON - subq_16.metric_time__day = subq_21.metric_time__day + cm_7_cte.metric_time__day = cm_8_cte.metric_time__day GROUP BY - COALESCE(subq_16.metric_time__day, subq_21.metric_time__day) + COALESCE(cm_7_cte.metric_time__day, cm_8_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_derived_metric__plan0_optimized.sql index 626212607..678c373c4 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_derived_metric__plan0_optimized.sql @@ -2,65 +2,87 @@ test_name: test_nested_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_17 +WITH cm_12_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_28.metric_time__day, subq_33.metric_time__day) AS metric_time__day - , MAX(subq_28.non_referred) AS non_referred - , MAX(subq_33.instant) AS instant - , MAX(subq_33.bookings) AS bookings + metric_time__day + , SUM(referred_bookings) AS ref_bookings + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred - FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(referred_bookings) AS ref_bookings - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - GROUP BY - metric_time__day - ) subq_27 - ) subq_28 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + GROUP BY + metric_time__day +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , SUM(instant_bookings) AS instant - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - GROUP BY - metric_time__day - ) subq_33 - ON - subq_28.metric_time__day = subq_33.metric_time__day + , ref_bookings + , bookings + FROM cm_12_cte cm_12_cte + ) subq_27 +) + +, cm_16_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(instant_bookings) AS instant + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 GROUP BY - COALESCE(subq_28.metric_time__day, subq_33.metric_time__day) -) subq_34 + metric_time__day +) + +, cm_17_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_13_cte.metric_time__day, cm_16_cte.metric_time__day) AS metric_time__day + , MAX(cm_13_cte.non_referred) AS non_referred + , MAX(cm_16_cte.instant) AS instant + , MAX(cm_16_cte.bookings) AS bookings + FROM cm_13_cte cm_13_cte + FULL OUTER JOIN + cm_16_cte cm_16_cte + ON + cm_13_cte.metric_time__day = cm_16_cte.metric_time__day + GROUP BY + COALESCE(cm_13_cte.metric_time__day, cm_16_cte.metric_time__day) + ) subq_34 +) + +SELECT + metric_time__day AS metric_time__day + , instant_plus_non_referred_bookings_pct AS instant_plus_non_referred_bookings_pct +FROM cm_17_cte cm_17_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql index 606a9fa31..e634f820a 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql @@ -2,57 +2,77 @@ test_name: test_nested_derived_metric_offset_with_joined_where_constraint_not_se test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS metric_time__day + , subq_15.booking__is_instant AS booking__is_instant + , SUM(subq_15.bookings) AS bookings + FROM ***************************.mf_time_spine subq_17 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + ON + DATEADD(day, -5, subq_17.ds) = subq_15.metric_time__day + GROUP BY + subq_17.ds + , subq_15.booking__is_instant +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking__is_instant + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , booking__is_instant + , bookings + FROM cm_6_cte cm_6_cte + ) subq_21 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__day - , bookings_offset_once + , 2 * bookings_offset_once AS bookings_offset_twice FROM ( - -- Join to Time Spine Dataset + -- Constrain Output with WHERE + -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] SELECT - subq_24.ds AS metric_time__day - , subq_22.booking__is_instant AS booking__is_instant - , subq_22.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_24 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , booking__is_instant - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_17.ds AS metric_time__day - , subq_15.booking__is_instant AS booking__is_instant - , SUM(subq_15.bookings) AS bookings - FROM ***************************.mf_time_spine subq_17 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - ON - DATEADD(day, -5, subq_17.ds) = subq_15.metric_time__day - GROUP BY - subq_17.ds - , subq_15.booking__is_instant - ) subq_21 - ) subq_22 - ON - DATEADD(day, -2, subq_24.ds) = subq_22.metric_time__day - ) subq_25 - WHERE booking__is_instant -) subq_27 + subq_24.ds AS metric_time__day + , cm_7_cte.booking__is_instant AS booking__is_instant + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_24 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATEADD(day, -2, subq_24.ds) = cm_7_cte.metric_time__day + ) subq_25 + WHERE booking__is_instant + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql index 38559a01a..d928c4311 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql @@ -2,65 +2,95 @@ test_name: test_nested_derived_metric_with_offset_multiple_input_metrics test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_14 +WITH cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_11_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month - , MAX(subq_30.booking_fees) AS booking_fees + metric_time__day + , booking_value * 0.05 AS booking_fees_start_of_month FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_10 SELECT - subq_23.ds AS metric_time__day - , subq_21.booking_fees_start_of_month AS booking_fees_start_of_month - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions - SELECT - metric_time__day - , booking_value * 0.05 AS booking_fees_start_of_month - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_20 - ) subq_21 - ON - DATE_TRUNC('month', subq_23.ds) = subq_21.metric_time__day - ) subq_24 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , booking_value + FROM cm_10_cte cm_10_cte + ) subq_20 +) + +, cm_12_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_value * 0.05 AS booking_fees + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , booking_value * 0.05 AS booking_fees + , booking_value + FROM cm_12_cte cm_12_cte + ) subq_29 +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_24.metric_time__day, cm_13_cte.metric_time__day) AS metric_time__day + , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month + , MAX(cm_13_cte.booking_fees) AS booking_fees FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_29 - ) subq_30 - ON - subq_24.metric_time__day = subq_30.metric_time__day - GROUP BY - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) -) subq_31 + subq_23.ds AS metric_time__day + , cm_11_cte.booking_fees_start_of_month AS booking_fees_start_of_month + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_11_cte cm_11_cte + ON + DATE_TRUNC('month', subq_23.ds) = cm_11_cte.metric_time__day + ) subq_24 + FULL OUTER JOIN + cm_13_cte cm_13_cte + ON + subq_24.metric_time__day = cm_13_cte.metric_time__day + GROUP BY + COALESCE(subq_24.metric_time__day, cm_13_cte.metric_time__day) + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , booking_fees_since_start_of_month AS booking_fees_since_start_of_month +FROM cm_14_cte cm_14_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql index 305efccf5..cd299bf77 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql @@ -2,36 +2,60 @@ test_name: test_nested_fill_nulls_without_time_spine test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_8 - GROUP BY - metric_time__day - ) subq_9 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_8 + GROUP BY + metric_time__day + ) subq_9 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_10 -) subq_11 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_7_cte cm_7_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , nested_fill_nulls_without_time_spine AS nested_fill_nulls_without_time_spine +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql index 06a0d6ccc..a293efeaf 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql @@ -3,46 +3,59 @@ test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_18.metric_time__day, subq_23.metric_time__day) AS metric_time__day - , MAX(subq_18.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_8_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine - FROM ( - -- Aggregate Measures - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_14 - GROUP BY - metric_time__day - ) subq_15 - ) subq_16 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_14 + GROUP BY + metric_time__day + ) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_8_cte cm_8_cte + ) subq_16 +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_9 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_9_cte cm_9_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_11_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -59,8 +72,16 @@ FULL OUTER JOIN ( ) subq_21 GROUP BY metric_time__day -) subq_23 +) + +SELECT + COALESCE(cm_10_cte.metric_time__day, cm_11_cte.metric_time__day) AS metric_time__day + , MAX(cm_10_cte.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine + , MAX(cm_11_cte.listings) AS listings +FROM cm_10_cte cm_10_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.metric_time__day = subq_23.metric_time__day + cm_10_cte.metric_time__day = cm_11_cte.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_23.metric_time__day) + COALESCE(cm_10_cte.metric_time__day, cm_11_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_filters__plan0_optimized.sql index 0cb09da10..154186c24 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_filters__plan0_optimized.sql @@ -4,67 +4,86 @@ docstring: Tests derived metric rendering for a nested derived metric with filters on the outer metric spec. sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - instant_lux_booking_value_rate AS instant_lux_booking_value_rate -FROM ( +-- Read From CTE For node_id=cm_15 +WITH cm_13_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings'] + -- Aggregate Measures -- Compute Metrics via Expressions SELECT - average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Combine Aggregated Outputs + -- Join Standard Outputs SELECT - MAX(subq_37.average_booking_value) AS average_booking_value - , MAX(subq_37.bookings) AS bookings - , MAX(subq_43.booking_value) AS booking_value + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_29.booking__is_instant AS booking__is_instant + , subq_29.bookings AS bookings + , subq_29.average_booking_value AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_29.booking__is_instant AS booking__is_instant - , subq_29.bookings AS bookings - , subq_29.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_29.listing = listings_latest_src_28000.listing_id - ) subq_33 - WHERE (listing__is_lux_latest) AND (booking__is_instant) - ) subq_37 - CROSS JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value',] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - SUM(booking_value) AS booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_39 - WHERE booking__is_instant - ) subq_43 + listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_29.listing = listings_latest_src_28000.listing_id + ) subq_33 + WHERE (listing__is_lux_latest) AND (booking__is_instant) +) + +, cm_12_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(booking_value) AS booking_value + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + is_instant AS booking__is_instant + , booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_39 + WHERE booking__is_instant +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(cm_13_cte.average_booking_value) AS average_booking_value + , MAX(cm_13_cte.bookings) AS bookings + , MAX(cm_12_cte.booking_value) AS booking_value + FROM cm_13_cte cm_13_cte + CROSS JOIN + cm_12_cte cm_12_cte ) subq_44 -) subq_45 +) + +, cm_15_cte AS ( + -- Compute Metrics via Expressions + SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate + FROM ( + -- Read From CTE For node_id=cm_14 + SELECT + instant_lux_booking_value_rate + FROM cm_14_cte cm_14_cte + ) subq_45 +) + +SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate +FROM cm_15_cte cm_15_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_offsets__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_offsets__plan0_optimized.sql index 858599a4b..841859aa4 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_offsets__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_offsets__plan0_optimized.sql @@ -2,44 +2,63 @@ test_name: test_nested_offsets test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_22 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - ON - DATEADD(day, -5, subq_15.ds) = subq_13.metric_time__day - GROUP BY - subq_15.ds - ) subq_19 - ) subq_20 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 ON - DATEADD(day, -2, subq_22.ds) = subq_20.metric_time__day -) subq_23 + DATEADD(day, -5, subq_15.ds) = subq_13.metric_time__day + GROUP BY + subq_15.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_22.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_22 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATEADD(day, -2, subq_22.ds) = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_offsets_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_offsets_with_time_constraint__plan0_optimized.sql index fef5b7164..0f74441d6 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_offsets_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_offsets_with_time_constraint__plan0_optimized.sql @@ -2,50 +2,69 @@ test_name: test_nested_offsets_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.metric_time__day AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ( - -- Time Spine - SELECT - ds AS metric_time__day - FROM ***************************.mf_time_spine subq_22 - WHERE ds BETWEEN '2020-01-12' AND '2020-01-13' - ) subq_21 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 + ON + DATEADD(day, -5, subq_15.ds) = subq_13.metric_time__day + GROUP BY + subq_15.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 SELECT metric_time__day - , 2 * bookings AS bookings_offset_once + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_21.metric_time__day AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Time Spine SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - ON - DATEADD(day, -5, subq_15.ds) = subq_13.metric_time__day - GROUP BY - subq_15.ds - ) subq_19 - ) subq_20 - ON - DATEADD(day, -2, subq_21.metric_time__day) = subq_20.metric_time__day -) subq_23 + ds AS metric_time__day + FROM ***************************.mf_time_spine subq_22 + WHERE ds BETWEEN '2020-01-12' AND '2020-01-13' + ) subq_21 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATEADD(day, -2, subq_21.metric_time__day) = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_offsets_with_where_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_offsets_with_where_constraint__plan0_optimized.sql index 3d6418dab..72096c894 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_offsets_with_where_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_offsets_with_where_constraint__plan0_optimized.sql @@ -2,51 +2,70 @@ test_name: test_nested_offsets_with_where_constraint test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_16.ds AS metric_time__day + , SUM(subq_14.bookings) AS bookings + FROM ***************************.mf_time_spine subq_16 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_14 + ON + DATEADD(day, -5, subq_16.ds) = subq_14.metric_time__day + GROUP BY + subq_16.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_20 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__day - , bookings_offset_once + , 2 * bookings_offset_once AS bookings_offset_twice FROM ( - -- Join to Time Spine Dataset + -- Constrain Output with WHERE SELECT - subq_23.ds AS metric_time__day - , subq_21.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_16.ds AS metric_time__day - , SUM(subq_14.bookings) AS bookings - FROM ***************************.mf_time_spine subq_16 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_14 - ON - DATEADD(day, -5, subq_16.ds) = subq_14.metric_time__day - GROUP BY - subq_16.ds - ) subq_20 - ) subq_21 - ON - DATEADD(day, -2, subq_23.ds) = subq_21.metric_time__day - ) subq_24 - WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' -) subq_25 + subq_23.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATEADD(day, -2, subq_23.ds) = cm_7_cte.metric_time__day + ) subq_24 + WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' + ) subq_25 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index e2ef45757..1bb3519f6 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,11 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with one granularity and filtered by a different one. sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__month'] -- Aggregate Measures @@ -38,4 +35,23 @@ FROM ( WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__month -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql index e47d7558a..34b6dc841 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql @@ -4,13 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with multiple granularities. sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day', 'metric_time__month', 'metric_time__year'] -- Aggregate Measures @@ -35,4 +30,29 @@ FROM ( subq_11.ds , DATE_TRUNC('month', subq_11.ds) , DATE_TRUNC('year', subq_11.ds) -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql index 3aefd9bee..cf445da59 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_to_grain_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.booking__ds__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + DATE_TRUNC('month', subq_22.ds) = subq_20.booking__ds__day GROUP BY - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index 2608a60ba..bf3d689ff 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,63 +4,76 @@ docstring: Test a query where an offset window metric is queried with one granularity and filtered by a different one. sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) AS metric_time__month - , MAX(subq_24.booking_value) AS booking_value - , MAX(subq_30.bookers) AS bookers + metric_time__month + , SUM(booking_value) AS booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__month - , SUM(booking_value) AS booking_value - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_19.ds AS metric_time__day - , DATE_TRUNC('month', subq_19.ds) AS metric_time__month - , bookings_source_src_28000.booking_value AS booking_value - FROM ***************************.mf_time_spine subq_19 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 - ON - DATEADD(week, -1, subq_19.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) - ) subq_20 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_24 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookers', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_19.ds AS metric_time__day + , DATE_TRUNC('month', subq_19.ds) AS metric_time__month + , bookings_source_src_28000.booking_value AS booking_value + FROM ***************************.mf_time_spine subq_19 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATEADD(week, -1, subq_19.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + ) subq_20 + WHERE metric_time__day = '2020-01-01' + GROUP BY + metric_time__month +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__month + , COUNT(DISTINCT bookers) AS bookers + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__month - , COUNT(DISTINCT bookers) AS bookers - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , DATE_TRUNC('month', ds) AS metric_time__month - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_30 - ON - subq_24.metric_time__month = subq_30.metric_time__month + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('month', ds) AS metric_time__month + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + WHERE metric_time__day = '2020-01-01' GROUP BY - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) -) subq_31 + metric_time__month +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + GROUP BY + COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) + ) subq_31 +) + +SELECT + metric_time__month AS metric_time__month + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_window_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_window_metric_multiple_granularities__plan0_optimized.sql index 22753736d..21a5403b9 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_window_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_window_metric_multiple_granularities__plan0_optimized.sql @@ -4,67 +4,82 @@ docstring: Test a query where an offset window metric is queried with multiple granularities. sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS metric_time__day + , DATE_TRUNC('month', subq_17.ds) AS metric_time__month + , DATE_TRUNC('year', subq_17.ds) AS metric_time__year + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATEADD(week, -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + GROUP BY + subq_17.ds + , DATE_TRUNC('month', subq_17.ds) + , DATE_TRUNC('year', subq_17.ds) +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('month', ds) AS metric_time__month + , DATE_TRUNC('year', ds) AS metric_time__year + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) + , DATE_TRUNC('month', ds) + , DATE_TRUNC('year', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) AS metric_time__month - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) AS metric_time__year - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + metric_time__day + , metric_time__month + , metric_time__year + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS metric_time__day - , DATE_TRUNC('month', subq_17.ds) AS metric_time__month - , DATE_TRUNC('year', subq_17.ds) AS metric_time__year - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - DATEADD(week, -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) AND ( + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + ) AND ( + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + ) GROUP BY - subq_17.ds - , DATE_TRUNC('month', subq_17.ds) - , DATE_TRUNC('year', subq_17.ds) - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , DATE_TRUNC('month', ds) AS metric_time__month - , DATE_TRUNC('year', ds) AS metric_time__year - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - , DATE_TRUNC('month', ds) - , DATE_TRUNC('year', ds) - ) subq_26 - ON - ( - subq_21.metric_time__day = subq_26.metric_time__day - ) AND ( - subq_21.metric_time__month = subq_26.metric_time__month - ) AND ( - subq_21.metric_time__year = subq_26.metric_time__year - ) - GROUP BY - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) -) subq_27 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + , COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) + , COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_window_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_window_with_agg_time_dim__plan0_optimized.sql index c0f274803..a23a8a2eb 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_window_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_window_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATEADD(day, -14, subq_22.ds) = subq_20.booking__ds__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + DATEADD(day, -14, subq_22.ds) = subq_20.booking__ds__day GROUP BY - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_time_offset_metric_with_time_constraint__plan0_optimized.sql index c855be0bd..b21949036 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -33,4 +30,23 @@ FROM ( DATEADD(day, -5, subq_10.metric_time__day) = subq_9.metric_time__day GROUP BY subq_10.metric_time__day -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql index 9aae37738..f7390f1cd 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_cumulative_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Constrain Time Range to [2019-12-19T00:00:00, 2020-01-02T00:00:00] -- Pass Only Elements: ['bookers', 'metric_time__day'] @@ -42,4 +39,23 @@ FROM ( WHERE subq_17.metric_time__day BETWEEN '2019-12-19' AND '2020-01-02' GROUP BY subq_17.metric_time__day -) subq_23 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric__plan0_optimized.sql index bafa9600f..600be0f69 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -25,4 +22,24 @@ FROM ( ) subq_13 GROUP BY metric_time__day -) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , ref_bookings + , bookings + FROM cm_8_cte cm_8_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , non_referred_bookings_pct AS non_referred_bookings_pct +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql index 024a2779d..91cae16e3 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql @@ -6,60 +6,81 @@ sql_engine: Snowflake --- -- Combine Aggregated Outputs -- Order By [] Limit 1 -SELECT - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) AS booking__is_instant - , MAX(subq_18.derived_shared_alias_1a) AS derived_shared_alias_1a - , MAX(subq_24.derived_shared_alias_2) AS derived_shared_alias_2 -FROM ( +WITH cm_8_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + GROUP BY + booking__is_instant +) + +, cm_9_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias - 10 AS derived_shared_alias_1a FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_8 SELECT booking__is_instant - , SUM(bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_8_cte cm_8_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_10_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(instant_bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_21 + GROUP BY + booking__is_instant +) + +, cm_11_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias + 10 AS derived_shared_alias_2 FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_10 SELECT booking__is_instant - , SUM(instant_bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_21 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_10_cte cm_10_cte ) subq_23 -) subq_24 +) + +SELECT + COALESCE(cm_9_cte.booking__is_instant, cm_11_cte.booking__is_instant) AS booking__is_instant + , MAX(cm_9_cte.derived_shared_alias_1a) AS derived_shared_alias_1a + , MAX(cm_11_cte.derived_shared_alias_2) AS derived_shared_alias_2 +FROM cm_9_cte cm_9_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.booking__is_instant = subq_24.booking__is_instant + cm_9_cte.booking__is_instant = cm_11_cte.booking__is_instant GROUP BY - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) + COALESCE(cm_9_cte.booking__is_instant, cm_11_cte.booking__is_instant) LIMIT 1 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql index 7b3141607..839d62208 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_month_dimension_and_offset_window test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_last_month AS bookings_last_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] -- Aggregate Measures @@ -28,4 +25,23 @@ FROM ( DATEADD(month, -1, subq_10.metric_time__month) = DATE_TRUNC('month', monthly_bookings_source_src_16000.ds) GROUP BY subq_10.metric_time__month -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_last_month AS bookings_last_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_last_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_last_month AS bookings_last_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_to_grain__plan0_optimized.sql index f61138cd3..a054addd3 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_to_grain__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql index 9d2b8b9ae..602dd5f5e 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,59 +2,72 @@ test_name: test_derived_metric_with_offset_to_grain_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) AS metric_time__week - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__week + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__week - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__week'] - SELECT - DATE_TRUNC('week', ds) AS metric_time__week - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__week - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__week'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATE_TRUNC('week', subq_22.ds) AS metric_time__week - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day - WHERE DATE_TRUNC('week', subq_22.ds) = subq_22.ds - GROUP BY - DATE_TRUNC('week', subq_22.ds) - ) subq_26 + DATE_TRUNC('week', ds) AS metric_time__week + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__week +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__week'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('week', subq_22.ds) AS metric_time__week + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__week = subq_26.metric_time__week + DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day + WHERE DATE_TRUNC('week', subq_22.ds) = subq_22.ds GROUP BY - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) -) subq_27 + DATE_TRUNC('week', subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__week, cm_7_cte.metric_time__week) AS metric_time__week + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__week = cm_7_cte.metric_time__week + GROUP BY + COALESCE(cm_6_cte.metric_time__week, cm_7_cte.metric_time__week) + ) subq_27 +) + +SELECT + metric_time__week AS metric_time__week + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window__plan0_optimized.sql index ce55aa295..4789c9e00 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql index 8b3679742..45961a46e 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__quarter - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) AS metric_time__quarter - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__quarter + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__quarter - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - SELECT - DATE_TRUNC('quarter', ds) AS metric_time__quarter - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__quarter - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATE_TRUNC('quarter', subq_22.ds) AS metric_time__quarter - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day - GROUP BY - DATE_TRUNC('quarter', subq_22.ds) - ) subq_26 + DATE_TRUNC('quarter', ds) AS metric_time__quarter + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__quarter +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__quarter'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('quarter', subq_22.ds) AS metric_time__quarter + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__quarter = subq_26.metric_time__quarter + DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) -) subq_27 + DATE_TRUNC('quarter', subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__quarter + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__quarter, cm_7_cte.metric_time__quarter) AS metric_time__quarter + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__quarter = cm_7_cte.metric_time__quarter + GROUP BY + COALESCE(cm_6_cte.metric_time__quarter, cm_7_cte.metric_time__quarter) + ) subq_27 +) + +SELECT + metric_time__quarter AS metric_time__quarter + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql index a19241a8b..4f95d5e7d 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql @@ -2,62 +2,75 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_20.ds AS metric_time__day + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds AS metric_time__day - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day - GROUP BY - subq_20.ds - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day + GROUP BY + subq_20.ds +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - DATEADD(month, -1, subq_28.ds) = subq_26.metric_time__day - GROUP BY - subq_28.ds - ) subq_32 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__day = subq_32.metric_time__day + DATEADD(month, -1, subq_28.ds) = subq_26.metric_time__day GROUP BY - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) -) subq_33 + subq_28.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql index b8eb112e7..e070709ed 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,63 +2,76 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain_and_granul test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__year - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) AS metric_time__year - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('year', subq_20.ds) AS metric_time__year + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATE_TRUNC('year', subq_20.ds) AS metric_time__year - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day - WHERE DATE_TRUNC('year', subq_20.ds) = subq_20.ds - GROUP BY - DATE_TRUNC('year', subq_20.ds) - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day + WHERE DATE_TRUNC('year', subq_20.ds) = subq_20.ds + GROUP BY + DATE_TRUNC('year', subq_20.ds) +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('year', subq_28.ds) AS metric_time__year + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATE_TRUNC('year', subq_28.ds) AS metric_time__year - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - DATEADD(month, -1, subq_28.ds) = subq_26.metric_time__day - GROUP BY - DATE_TRUNC('year', subq_28.ds) - ) subq_32 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__year = subq_32.metric_time__year + DATEADD(month, -1, subq_28.ds) = subq_26.metric_time__day GROUP BY - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) -) subq_33 + DATE_TRUNC('year', subq_28.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__year + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + GROUP BY + COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) + ) subq_33 +) + +SELECT + metric_time__year AS metric_time__year + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql index f8f10fb73..649e6b95f 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_derived_metric_with_offset_window_and_time_filter test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_17 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_21.bookings) AS bookings - , MAX(subq_30.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings_2_weeks_ago FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( + subq_25.ds AS metric_time__day + , subq_23.bookings AS bookings + FROM ***************************.mf_time_spine subq_25 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT DATE_TRUNC('day', ds) AS metric_time__day , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_17 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' - GROUP BY - metric_time__day - ) subq_21 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + ) subq_23 + ON + DATEADD(day, -14, subq_25.ds) = subq_23.metric_time__day + ) subq_26 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_25.ds AS metric_time__day - , subq_23.bookings AS bookings - FROM ***************************.mf_time_spine subq_25 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - ON - DATEADD(day, -14, subq_25.ds) = subq_23.metric_time__day - ) subq_26 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day GROUP BY - metric_time__day - ) subq_30 - ON - subq_21.metric_time__day = subq_30.metric_time__day - GROUP BY - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) -) subq_31 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_offset_cumulative_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_offset_cumulative_metric__plan0_optimized.sql index 3776496f5..3e53cd4d0 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_offset_cumulative_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_offset_cumulative_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_cumulative_metric test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookers', 'metric_time__day'] -- Aggregate Measures @@ -34,4 +31,23 @@ FROM ( DATEADD(day, -2, subq_17.ds) = subq_15.metric_time__day GROUP BY subq_17.ds -) subq_21 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_21 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql index 88b862779..18d8866cf 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql @@ -2,47 +2,60 @@ test_name: test_derived_offset_metric_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS booking__ds__day + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATEADD(week, -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + GROUP BY + subq_17.ds +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + booking__ds__day + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS booking__ds__day - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - DATEADD(week, -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day GROUP BY - subq_17.ds - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_26 - ON - subq_21.booking__ds__day = subq_26.booking__ds__day - GROUP BY - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql index a603ed6aa..b1cef9e33 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_metric_with_one_input_metric test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATEADD(day, -5, subq_11.ds) = subq_9.metric_time__day GROUP BY subq_11.ds -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_multi_metric_fill_null__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_multi_metric_fill_null__plan0_optimized.sql index ca0e3a997..5a09db692 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_multi_metric_fill_null__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_multi_metric_fill_null__plan0_optimized.sql @@ -3,40 +3,45 @@ test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_16.metric_time__day, subq_21.metric_time__day) AS metric_time__day - , MAX(subq_16.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine - , MAX(subq_21.listings) AS listings -FROM ( +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - GROUP BY - metric_time__day - ) subq_14 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 + GROUP BY + metric_time__day + ) subq_14 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_15 -) subq_16 -FULL OUTER JOIN ( +) + +, cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -53,8 +58,16 @@ FULL OUTER JOIN ( ) subq_19 GROUP BY metric_time__day -) subq_21 +) + +SELECT + COALESCE(cm_7_cte.metric_time__day, cm_8_cte.metric_time__day) AS metric_time__day + , MAX(cm_7_cte.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine + , MAX(cm_8_cte.listings) AS listings +FROM cm_7_cte cm_7_cte +FULL OUTER JOIN + cm_8_cte cm_8_cte ON - subq_16.metric_time__day = subq_21.metric_time__day + cm_7_cte.metric_time__day = cm_8_cte.metric_time__day GROUP BY - COALESCE(subq_16.metric_time__day, subq_21.metric_time__day) + COALESCE(cm_7_cte.metric_time__day, cm_8_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_derived_metric__plan0_optimized.sql index f48b4f909..a68802f10 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_derived_metric__plan0_optimized.sql @@ -2,65 +2,87 @@ test_name: test_nested_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_17 +WITH cm_12_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_28.metric_time__day, subq_33.metric_time__day) AS metric_time__day - , MAX(subq_28.non_referred) AS non_referred - , MAX(subq_33.instant) AS instant - , MAX(subq_33.bookings) AS bookings + metric_time__day + , SUM(referred_bookings) AS ref_bookings + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred - FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(referred_bookings) AS ref_bookings - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - GROUP BY - metric_time__day - ) subq_27 - ) subq_28 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + GROUP BY + metric_time__day +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , SUM(instant_bookings) AS instant - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - GROUP BY - metric_time__day - ) subq_33 - ON - subq_28.metric_time__day = subq_33.metric_time__day + , ref_bookings + , bookings + FROM cm_12_cte cm_12_cte + ) subq_27 +) + +, cm_16_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(instant_bookings) AS instant + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 GROUP BY - COALESCE(subq_28.metric_time__day, subq_33.metric_time__day) -) subq_34 + metric_time__day +) + +, cm_17_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_13_cte.metric_time__day, cm_16_cte.metric_time__day) AS metric_time__day + , MAX(cm_13_cte.non_referred) AS non_referred + , MAX(cm_16_cte.instant) AS instant + , MAX(cm_16_cte.bookings) AS bookings + FROM cm_13_cte cm_13_cte + FULL OUTER JOIN + cm_16_cte cm_16_cte + ON + cm_13_cte.metric_time__day = cm_16_cte.metric_time__day + GROUP BY + COALESCE(cm_13_cte.metric_time__day, cm_16_cte.metric_time__day) + ) subq_34 +) + +SELECT + metric_time__day AS metric_time__day + , instant_plus_non_referred_bookings_pct AS instant_plus_non_referred_bookings_pct +FROM cm_17_cte cm_17_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql index 9388c1fcc..f5bba9bd4 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql @@ -2,57 +2,77 @@ test_name: test_nested_derived_metric_offset_with_joined_where_constraint_not_se test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS metric_time__day + , subq_15.booking__is_instant AS booking__is_instant + , SUM(subq_15.bookings) AS bookings + FROM ***************************.mf_time_spine subq_17 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + ON + DATEADD(day, -5, subq_17.ds) = subq_15.metric_time__day + GROUP BY + subq_17.ds + , subq_15.booking__is_instant +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking__is_instant + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , booking__is_instant + , bookings + FROM cm_6_cte cm_6_cte + ) subq_21 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__day - , bookings_offset_once + , 2 * bookings_offset_once AS bookings_offset_twice FROM ( - -- Join to Time Spine Dataset + -- Constrain Output with WHERE + -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] SELECT - subq_24.ds AS metric_time__day - , subq_22.booking__is_instant AS booking__is_instant - , subq_22.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_24 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , booking__is_instant - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_17.ds AS metric_time__day - , subq_15.booking__is_instant AS booking__is_instant - , SUM(subq_15.bookings) AS bookings - FROM ***************************.mf_time_spine subq_17 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - ON - DATEADD(day, -5, subq_17.ds) = subq_15.metric_time__day - GROUP BY - subq_17.ds - , subq_15.booking__is_instant - ) subq_21 - ) subq_22 - ON - DATEADD(day, -2, subq_24.ds) = subq_22.metric_time__day - ) subq_25 - WHERE booking__is_instant -) subq_27 + subq_24.ds AS metric_time__day + , cm_7_cte.booking__is_instant AS booking__is_instant + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_24 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATEADD(day, -2, subq_24.ds) = cm_7_cte.metric_time__day + ) subq_25 + WHERE booking__is_instant + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql index 58da2a3cc..4dec1d1bc 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql @@ -2,65 +2,95 @@ test_name: test_nested_derived_metric_with_offset_multiple_input_metrics test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_14 +WITH cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_11_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month - , MAX(subq_30.booking_fees) AS booking_fees + metric_time__day + , booking_value * 0.05 AS booking_fees_start_of_month FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_10 SELECT - subq_23.ds AS metric_time__day - , subq_21.booking_fees_start_of_month AS booking_fees_start_of_month - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions - SELECT - metric_time__day - , booking_value * 0.05 AS booking_fees_start_of_month - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_20 - ) subq_21 - ON - DATE_TRUNC('month', subq_23.ds) = subq_21.metric_time__day - ) subq_24 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , booking_value + FROM cm_10_cte cm_10_cte + ) subq_20 +) + +, cm_12_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_value * 0.05 AS booking_fees + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , booking_value * 0.05 AS booking_fees + , booking_value + FROM cm_12_cte cm_12_cte + ) subq_29 +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_24.metric_time__day, cm_13_cte.metric_time__day) AS metric_time__day + , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month + , MAX(cm_13_cte.booking_fees) AS booking_fees FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_29 - ) subq_30 - ON - subq_24.metric_time__day = subq_30.metric_time__day - GROUP BY - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) -) subq_31 + subq_23.ds AS metric_time__day + , cm_11_cte.booking_fees_start_of_month AS booking_fees_start_of_month + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_11_cte cm_11_cte + ON + DATE_TRUNC('month', subq_23.ds) = cm_11_cte.metric_time__day + ) subq_24 + FULL OUTER JOIN + cm_13_cte cm_13_cte + ON + subq_24.metric_time__day = cm_13_cte.metric_time__day + GROUP BY + COALESCE(subq_24.metric_time__day, cm_13_cte.metric_time__day) + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , booking_fees_since_start_of_month AS booking_fees_since_start_of_month +FROM cm_14_cte cm_14_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql index a123a96e5..37ade1899 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql @@ -2,36 +2,60 @@ test_name: test_nested_fill_nulls_without_time_spine test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_8 - GROUP BY - metric_time__day - ) subq_9 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_8 + GROUP BY + metric_time__day + ) subq_9 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_10 -) subq_11 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_7_cte cm_7_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , nested_fill_nulls_without_time_spine AS nested_fill_nulls_without_time_spine +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql index ff990f19b..dd51fae90 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql @@ -3,46 +3,59 @@ test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_18.metric_time__day, subq_23.metric_time__day) AS metric_time__day - , MAX(subq_18.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_8_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine - FROM ( - -- Aggregate Measures - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_14 - GROUP BY - metric_time__day - ) subq_15 - ) subq_16 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_14 + GROUP BY + metric_time__day + ) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_8_cte cm_8_cte + ) subq_16 +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_9 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_9_cte cm_9_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_11_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -59,8 +72,16 @@ FULL OUTER JOIN ( ) subq_21 GROUP BY metric_time__day -) subq_23 +) + +SELECT + COALESCE(cm_10_cte.metric_time__day, cm_11_cte.metric_time__day) AS metric_time__day + , MAX(cm_10_cte.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine + , MAX(cm_11_cte.listings) AS listings +FROM cm_10_cte cm_10_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.metric_time__day = subq_23.metric_time__day + cm_10_cte.metric_time__day = cm_11_cte.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_23.metric_time__day) + COALESCE(cm_10_cte.metric_time__day, cm_11_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_filters__plan0_optimized.sql index c070ddafd..9011bf50c 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_filters__plan0_optimized.sql @@ -4,67 +4,86 @@ docstring: Tests derived metric rendering for a nested derived metric with filters on the outer metric spec. sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - instant_lux_booking_value_rate AS instant_lux_booking_value_rate -FROM ( +-- Read From CTE For node_id=cm_15 +WITH cm_13_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings'] + -- Aggregate Measures -- Compute Metrics via Expressions SELECT - average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Combine Aggregated Outputs + -- Join Standard Outputs SELECT - MAX(subq_37.average_booking_value) AS average_booking_value - , MAX(subq_37.bookings) AS bookings - , MAX(subq_43.booking_value) AS booking_value + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_29.booking__is_instant AS booking__is_instant + , subq_29.bookings AS bookings + , subq_29.average_booking_value AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_29.booking__is_instant AS booking__is_instant - , subq_29.bookings AS bookings - , subq_29.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_29.listing = listings_latest_src_28000.listing_id - ) subq_33 - WHERE (listing__is_lux_latest) AND (booking__is_instant) - ) subq_37 - CROSS JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value',] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - SUM(booking_value) AS booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_39 - WHERE booking__is_instant - ) subq_43 + listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_29.listing = listings_latest_src_28000.listing_id + ) subq_33 + WHERE (listing__is_lux_latest) AND (booking__is_instant) +) + +, cm_12_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(booking_value) AS booking_value + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + is_instant AS booking__is_instant + , booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_39 + WHERE booking__is_instant +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(cm_13_cte.average_booking_value) AS average_booking_value + , MAX(cm_13_cte.bookings) AS bookings + , MAX(cm_12_cte.booking_value) AS booking_value + FROM cm_13_cte cm_13_cte + CROSS JOIN + cm_12_cte cm_12_cte ) subq_44 -) subq_45 +) + +, cm_15_cte AS ( + -- Compute Metrics via Expressions + SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate + FROM ( + -- Read From CTE For node_id=cm_14 + SELECT + instant_lux_booking_value_rate + FROM cm_14_cte cm_14_cte + ) subq_45 +) + +SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate +FROM cm_15_cte cm_15_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_offsets__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_offsets__plan0_optimized.sql index f750dc648..37be9a8fe 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_offsets__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_offsets__plan0_optimized.sql @@ -2,44 +2,63 @@ test_name: test_nested_offsets test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_22 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - ON - DATEADD(day, -5, subq_15.ds) = subq_13.metric_time__day - GROUP BY - subq_15.ds - ) subq_19 - ) subq_20 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 ON - DATEADD(day, -2, subq_22.ds) = subq_20.metric_time__day -) subq_23 + DATEADD(day, -5, subq_15.ds) = subq_13.metric_time__day + GROUP BY + subq_15.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_22.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_22 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATEADD(day, -2, subq_22.ds) = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_offsets_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_offsets_with_time_constraint__plan0_optimized.sql index dc5ec3de8..86a384f13 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_offsets_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_offsets_with_time_constraint__plan0_optimized.sql @@ -2,50 +2,69 @@ test_name: test_nested_offsets_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.metric_time__day AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ( - -- Time Spine - SELECT - ds AS metric_time__day - FROM ***************************.mf_time_spine subq_22 - WHERE ds BETWEEN '2020-01-12' AND '2020-01-13' - ) subq_21 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 + ON + DATEADD(day, -5, subq_15.ds) = subq_13.metric_time__day + GROUP BY + subq_15.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 SELECT metric_time__day - , 2 * bookings AS bookings_offset_once + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_21.metric_time__day AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Time Spine SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - ON - DATEADD(day, -5, subq_15.ds) = subq_13.metric_time__day - GROUP BY - subq_15.ds - ) subq_19 - ) subq_20 - ON - DATEADD(day, -2, subq_21.metric_time__day) = subq_20.metric_time__day -) subq_23 + ds AS metric_time__day + FROM ***************************.mf_time_spine subq_22 + WHERE ds BETWEEN '2020-01-12' AND '2020-01-13' + ) subq_21 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATEADD(day, -2, subq_21.metric_time__day) = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_offsets_with_where_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_offsets_with_where_constraint__plan0_optimized.sql index d23fbf4a1..dabae48a7 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_offsets_with_where_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_offsets_with_where_constraint__plan0_optimized.sql @@ -2,51 +2,70 @@ test_name: test_nested_offsets_with_where_constraint test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_16.ds AS metric_time__day + , SUM(subq_14.bookings) AS bookings + FROM ***************************.mf_time_spine subq_16 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_14 + ON + DATEADD(day, -5, subq_16.ds) = subq_14.metric_time__day + GROUP BY + subq_16.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_20 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__day - , bookings_offset_once + , 2 * bookings_offset_once AS bookings_offset_twice FROM ( - -- Join to Time Spine Dataset + -- Constrain Output with WHERE SELECT - subq_23.ds AS metric_time__day - , subq_21.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_16.ds AS metric_time__day - , SUM(subq_14.bookings) AS bookings - FROM ***************************.mf_time_spine subq_16 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_14 - ON - DATEADD(day, -5, subq_16.ds) = subq_14.metric_time__day - GROUP BY - subq_16.ds - ) subq_20 - ) subq_21 - ON - DATEADD(day, -2, subq_23.ds) = subq_21.metric_time__day - ) subq_24 - WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' -) subq_25 + subq_23.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATEADD(day, -2, subq_23.ds) = cm_7_cte.metric_time__day + ) subq_24 + WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' + ) subq_25 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index 2fec95707..2df51cb20 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,11 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with one granularity and filtered by a different one. sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__month'] -- Aggregate Measures @@ -38,4 +35,23 @@ FROM ( WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__month -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql index d270f2280..6c1c35fbe 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql @@ -4,13 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with multiple granularities. sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day', 'metric_time__month', 'metric_time__year'] -- Aggregate Measures @@ -35,4 +30,29 @@ FROM ( subq_11.ds , DATE_TRUNC('month', subq_11.ds) , DATE_TRUNC('year', subq_11.ds) -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql index cc5e1978f..c8831345b 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_to_grain_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.booking__ds__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + DATE_TRUNC('month', subq_22.ds) = subq_20.booking__ds__day GROUP BY - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index 28159c151..478f5d509 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,63 +4,76 @@ docstring: Test a query where an offset window metric is queried with one granularity and filtered by a different one. sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) AS metric_time__month - , MAX(subq_24.booking_value) AS booking_value - , MAX(subq_30.bookers) AS bookers + metric_time__month + , SUM(booking_value) AS booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__month - , SUM(booking_value) AS booking_value - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_19.ds AS metric_time__day - , DATE_TRUNC('month', subq_19.ds) AS metric_time__month - , bookings_source_src_28000.booking_value AS booking_value - FROM ***************************.mf_time_spine subq_19 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 - ON - DATEADD(week, -1, subq_19.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) - ) subq_20 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_24 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookers', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_19.ds AS metric_time__day + , DATE_TRUNC('month', subq_19.ds) AS metric_time__month + , bookings_source_src_28000.booking_value AS booking_value + FROM ***************************.mf_time_spine subq_19 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATEADD(week, -1, subq_19.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + ) subq_20 + WHERE metric_time__day = '2020-01-01' + GROUP BY + metric_time__month +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__month + , COUNT(DISTINCT bookers) AS bookers + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__month - , COUNT(DISTINCT bookers) AS bookers - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , DATE_TRUNC('month', ds) AS metric_time__month - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_30 - ON - subq_24.metric_time__month = subq_30.metric_time__month + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('month', ds) AS metric_time__month + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + WHERE metric_time__day = '2020-01-01' GROUP BY - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) -) subq_31 + metric_time__month +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + GROUP BY + COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) + ) subq_31 +) + +SELECT + metric_time__month AS metric_time__month + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_metric_multiple_granularities__plan0_optimized.sql index 1e3460920..1815f1c48 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_metric_multiple_granularities__plan0_optimized.sql @@ -4,67 +4,82 @@ docstring: Test a query where an offset window metric is queried with multiple granularities. sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS metric_time__day + , DATE_TRUNC('month', subq_17.ds) AS metric_time__month + , DATE_TRUNC('year', subq_17.ds) AS metric_time__year + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATEADD(week, -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + GROUP BY + subq_17.ds + , DATE_TRUNC('month', subq_17.ds) + , DATE_TRUNC('year', subq_17.ds) +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('month', ds) AS metric_time__month + , DATE_TRUNC('year', ds) AS metric_time__year + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) + , DATE_TRUNC('month', ds) + , DATE_TRUNC('year', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) AS metric_time__month - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) AS metric_time__year - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + metric_time__day + , metric_time__month + , metric_time__year + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS metric_time__day - , DATE_TRUNC('month', subq_17.ds) AS metric_time__month - , DATE_TRUNC('year', subq_17.ds) AS metric_time__year - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - DATEADD(week, -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) AND ( + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + ) AND ( + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + ) GROUP BY - subq_17.ds - , DATE_TRUNC('month', subq_17.ds) - , DATE_TRUNC('year', subq_17.ds) - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , DATE_TRUNC('month', ds) AS metric_time__month - , DATE_TRUNC('year', ds) AS metric_time__year - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - , DATE_TRUNC('month', ds) - , DATE_TRUNC('year', ds) - ) subq_26 - ON - ( - subq_21.metric_time__day = subq_26.metric_time__day - ) AND ( - subq_21.metric_time__month = subq_26.metric_time__month - ) AND ( - subq_21.metric_time__year = subq_26.metric_time__year - ) - GROUP BY - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) -) subq_27 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + , COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) + , COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_with_agg_time_dim__plan0_optimized.sql index bc37fe731..ca63443fb 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATEADD(day, -14, subq_22.ds) = subq_20.booking__ds__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + DATEADD(day, -14, subq_22.ds) = subq_20.booking__ds__day GROUP BY - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_time_offset_metric_with_time_constraint__plan0_optimized.sql index d1eb7907e..87c161097 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -33,4 +30,23 @@ FROM ( DATEADD(day, -5, subq_10.metric_time__day) = subq_9.metric_time__day GROUP BY subq_10.metric_time__day -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql index 2026fd215..57d0ce2a8 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_cumulative_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Constrain Time Range to [2019-12-19T00:00:00, 2020-01-02T00:00:00] -- Pass Only Elements: ['bookers', 'metric_time__day'] @@ -42,4 +39,23 @@ FROM ( WHERE subq_17.metric_time__day BETWEEN timestamp '2019-12-19' AND timestamp '2020-01-02' GROUP BY subq_17.metric_time__day -) subq_23 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric__plan0_optimized.sql index 3521bdd8f..6d0092b91 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -25,4 +22,24 @@ FROM ( ) subq_13 GROUP BY metric_time__day -) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , ref_bookings + , bookings + FROM cm_8_cte cm_8_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , non_referred_bookings_pct AS non_referred_bookings_pct +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql index 3a8340d23..382b266e7 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql @@ -6,60 +6,81 @@ sql_engine: Trino --- -- Combine Aggregated Outputs -- Order By [] Limit 1 -SELECT - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) AS booking__is_instant - , MAX(subq_18.derived_shared_alias_1a) AS derived_shared_alias_1a - , MAX(subq_24.derived_shared_alias_2) AS derived_shared_alias_2 -FROM ( +WITH cm_8_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + GROUP BY + booking__is_instant +) + +, cm_9_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias - 10 AS derived_shared_alias_1a FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_8 SELECT booking__is_instant - , SUM(bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_8_cte cm_8_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_10_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(instant_bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_21 + GROUP BY + booking__is_instant +) + +, cm_11_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias + 10 AS derived_shared_alias_2 FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_10 SELECT booking__is_instant - , SUM(instant_bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_21 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_10_cte cm_10_cte ) subq_23 -) subq_24 +) + +SELECT + COALESCE(cm_9_cte.booking__is_instant, cm_11_cte.booking__is_instant) AS booking__is_instant + , MAX(cm_9_cte.derived_shared_alias_1a) AS derived_shared_alias_1a + , MAX(cm_11_cte.derived_shared_alias_2) AS derived_shared_alias_2 +FROM cm_9_cte cm_9_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.booking__is_instant = subq_24.booking__is_instant + cm_9_cte.booking__is_instant = cm_11_cte.booking__is_instant GROUP BY - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) + COALESCE(cm_9_cte.booking__is_instant, cm_11_cte.booking__is_instant) LIMIT 1 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql index 88c79149c..5b1a9cf3f 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_month_dimension_and_offset_window test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_last_month AS bookings_last_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] -- Aggregate Measures @@ -28,4 +25,23 @@ FROM ( DATE_ADD('month', -1, subq_10.metric_time__month) = DATE_TRUNC('month', monthly_bookings_source_src_16000.ds) GROUP BY subq_10.metric_time__month -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_last_month AS bookings_last_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_last_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_last_month AS bookings_last_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_to_grain__plan0_optimized.sql index 7c14527ad..a92457cfc 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_to_grain__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql index 20579c629..2c325cacf 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,59 +2,72 @@ test_name: test_derived_metric_with_offset_to_grain_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) AS metric_time__week - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__week + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__week - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__week'] - SELECT - DATE_TRUNC('week', ds) AS metric_time__week - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__week - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__week'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATE_TRUNC('week', subq_22.ds) AS metric_time__week - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day - WHERE DATE_TRUNC('week', subq_22.ds) = subq_22.ds - GROUP BY - DATE_TRUNC('week', subq_22.ds) - ) subq_26 + DATE_TRUNC('week', ds) AS metric_time__week + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__week +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__week'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('week', subq_22.ds) AS metric_time__week + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__week = subq_26.metric_time__week + DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day + WHERE DATE_TRUNC('week', subq_22.ds) = subq_22.ds GROUP BY - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) -) subq_27 + DATE_TRUNC('week', subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__week, cm_7_cte.metric_time__week) AS metric_time__week + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__week = cm_7_cte.metric_time__week + GROUP BY + COALESCE(cm_6_cte.metric_time__week, cm_7_cte.metric_time__week) + ) subq_27 +) + +SELECT + metric_time__week AS metric_time__week + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window__plan0_optimized.sql index 97562a5c6..363d41e3e 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_ADD('day', -14, subq_22.ds) = subq_20.metric_time__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + DATE_ADD('day', -14, subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql index a92f24882..fdf498287 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__quarter - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) AS metric_time__quarter - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__quarter + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__quarter - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - SELECT - DATE_TRUNC('quarter', ds) AS metric_time__quarter - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__quarter - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATE_TRUNC('quarter', subq_22.ds) AS metric_time__quarter - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_ADD('day', -14, subq_22.ds) = subq_20.metric_time__day - GROUP BY - DATE_TRUNC('quarter', subq_22.ds) - ) subq_26 + DATE_TRUNC('quarter', ds) AS metric_time__quarter + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__quarter +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__quarter'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('quarter', subq_22.ds) AS metric_time__quarter + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__quarter = subq_26.metric_time__quarter + DATE_ADD('day', -14, subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) -) subq_27 + DATE_TRUNC('quarter', subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__quarter + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__quarter, cm_7_cte.metric_time__quarter) AS metric_time__quarter + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__quarter = cm_7_cte.metric_time__quarter + GROUP BY + COALESCE(cm_6_cte.metric_time__quarter, cm_7_cte.metric_time__quarter) + ) subq_27 +) + +SELECT + metric_time__quarter AS metric_time__quarter + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql index 320c34869..3e924f48f 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql @@ -2,62 +2,75 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_20.ds AS metric_time__day + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds AS metric_time__day - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day - GROUP BY - subq_20.ds - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day + GROUP BY + subq_20.ds +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - DATE_ADD('month', -1, subq_28.ds) = subq_26.metric_time__day - GROUP BY - subq_28.ds - ) subq_32 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__day = subq_32.metric_time__day + DATE_ADD('month', -1, subq_28.ds) = subq_26.metric_time__day GROUP BY - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) -) subq_33 + subq_28.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql index 376523e66..68a5cf630 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,63 +2,76 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain_and_granul test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__year - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) AS metric_time__year - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('year', subq_20.ds) AS metric_time__year + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATE_TRUNC('year', subq_20.ds) AS metric_time__year - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day - WHERE DATE_TRUNC('year', subq_20.ds) = subq_20.ds - GROUP BY - DATE_TRUNC('year', subq_20.ds) - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day + WHERE DATE_TRUNC('year', subq_20.ds) = subq_20.ds + GROUP BY + DATE_TRUNC('year', subq_20.ds) +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('year', subq_28.ds) AS metric_time__year + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATE_TRUNC('year', subq_28.ds) AS metric_time__year - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - DATE_ADD('month', -1, subq_28.ds) = subq_26.metric_time__day - GROUP BY - DATE_TRUNC('year', subq_28.ds) - ) subq_32 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__year = subq_32.metric_time__year + DATE_ADD('month', -1, subq_28.ds) = subq_26.metric_time__day GROUP BY - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) -) subq_33 + DATE_TRUNC('year', subq_28.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__year + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + GROUP BY + COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) + ) subq_33 +) + +SELECT + metric_time__year AS metric_time__year + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql index deaef7874..b5ed9b0f8 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_derived_metric_with_offset_window_and_time_filter test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_17 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_21.bookings) AS bookings - , MAX(subq_30.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings_2_weeks_ago FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( + subq_25.ds AS metric_time__day + , subq_23.bookings AS bookings + FROM ***************************.mf_time_spine subq_25 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT DATE_TRUNC('day', ds) AS metric_time__day , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_17 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' - GROUP BY - metric_time__day - ) subq_21 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + ) subq_23 + ON + DATE_ADD('day', -14, subq_25.ds) = subq_23.metric_time__day + ) subq_26 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_25.ds AS metric_time__day - , subq_23.bookings AS bookings - FROM ***************************.mf_time_spine subq_25 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - ON - DATE_ADD('day', -14, subq_25.ds) = subq_23.metric_time__day - ) subq_26 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day GROUP BY - metric_time__day - ) subq_30 - ON - subq_21.metric_time__day = subq_30.metric_time__day - GROUP BY - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) -) subq_31 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_offset_cumulative_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_offset_cumulative_metric__plan0_optimized.sql index c9431a6e5..6cec2c51f 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_offset_cumulative_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_offset_cumulative_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_cumulative_metric test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookers', 'metric_time__day'] -- Aggregate Measures @@ -34,4 +31,23 @@ FROM ( DATE_ADD('day', -2, subq_17.ds) = subq_15.metric_time__day GROUP BY subq_17.ds -) subq_21 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_21 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql index bf4161748..885edc783 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql @@ -2,47 +2,60 @@ test_name: test_derived_offset_metric_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS booking__ds__day + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATE_ADD('week', -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + GROUP BY + subq_17.ds +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + booking__ds__day + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS booking__ds__day - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - DATE_ADD('week', -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day GROUP BY - subq_17.ds - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_26 - ON - subq_21.booking__ds__day = subq_26.booking__ds__day - GROUP BY - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql index bb78d05bf..ea8de1996 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_metric_with_one_input_metric test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATE_ADD('day', -5, subq_11.ds) = subq_9.metric_time__day GROUP BY subq_11.ds -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_multi_metric_fill_null__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_multi_metric_fill_null__plan0_optimized.sql index 426c0682a..1a3790802 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_multi_metric_fill_null__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_multi_metric_fill_null__plan0_optimized.sql @@ -3,40 +3,45 @@ test_filename: test_derived_metric_rendering.py sql_engine: Trino --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_16.metric_time__day, subq_21.metric_time__day) AS metric_time__day - , MAX(subq_16.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine - , MAX(subq_21.listings) AS listings -FROM ( +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - GROUP BY - metric_time__day - ) subq_14 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 + GROUP BY + metric_time__day + ) subq_14 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_15 -) subq_16 -FULL OUTER JOIN ( +) + +, cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -53,8 +58,16 @@ FULL OUTER JOIN ( ) subq_19 GROUP BY metric_time__day -) subq_21 +) + +SELECT + COALESCE(cm_7_cte.metric_time__day, cm_8_cte.metric_time__day) AS metric_time__day + , MAX(cm_7_cte.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine + , MAX(cm_8_cte.listings) AS listings +FROM cm_7_cte cm_7_cte +FULL OUTER JOIN + cm_8_cte cm_8_cte ON - subq_16.metric_time__day = subq_21.metric_time__day + cm_7_cte.metric_time__day = cm_8_cte.metric_time__day GROUP BY - COALESCE(subq_16.metric_time__day, subq_21.metric_time__day) + COALESCE(cm_7_cte.metric_time__day, cm_8_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_derived_metric__plan0_optimized.sql index d0deaa9a1..a02eddf1e 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_derived_metric__plan0_optimized.sql @@ -2,65 +2,87 @@ test_name: test_nested_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_17 +WITH cm_12_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_28.metric_time__day, subq_33.metric_time__day) AS metric_time__day - , MAX(subq_28.non_referred) AS non_referred - , MAX(subq_33.instant) AS instant - , MAX(subq_33.bookings) AS bookings + metric_time__day + , SUM(referred_bookings) AS ref_bookings + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred - FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(referred_bookings) AS ref_bookings - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - GROUP BY - metric_time__day - ) subq_27 - ) subq_28 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + GROUP BY + metric_time__day +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , SUM(instant_bookings) AS instant - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - GROUP BY - metric_time__day - ) subq_33 - ON - subq_28.metric_time__day = subq_33.metric_time__day + , ref_bookings + , bookings + FROM cm_12_cte cm_12_cte + ) subq_27 +) + +, cm_16_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(instant_bookings) AS instant + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 GROUP BY - COALESCE(subq_28.metric_time__day, subq_33.metric_time__day) -) subq_34 + metric_time__day +) + +, cm_17_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_13_cte.metric_time__day, cm_16_cte.metric_time__day) AS metric_time__day + , MAX(cm_13_cte.non_referred) AS non_referred + , MAX(cm_16_cte.instant) AS instant + , MAX(cm_16_cte.bookings) AS bookings + FROM cm_13_cte cm_13_cte + FULL OUTER JOIN + cm_16_cte cm_16_cte + ON + cm_13_cte.metric_time__day = cm_16_cte.metric_time__day + GROUP BY + COALESCE(cm_13_cte.metric_time__day, cm_16_cte.metric_time__day) + ) subq_34 +) + +SELECT + metric_time__day AS metric_time__day + , instant_plus_non_referred_bookings_pct AS instant_plus_non_referred_bookings_pct +FROM cm_17_cte cm_17_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql index 8adb624ac..614ec2a12 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql @@ -2,57 +2,77 @@ test_name: test_nested_derived_metric_offset_with_joined_where_constraint_not_se test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS metric_time__day + , subq_15.booking__is_instant AS booking__is_instant + , SUM(subq_15.bookings) AS bookings + FROM ***************************.mf_time_spine subq_17 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + ON + DATE_ADD('day', -5, subq_17.ds) = subq_15.metric_time__day + GROUP BY + subq_17.ds + , subq_15.booking__is_instant +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking__is_instant + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , booking__is_instant + , bookings + FROM cm_6_cte cm_6_cte + ) subq_21 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__day - , bookings_offset_once + , 2 * bookings_offset_once AS bookings_offset_twice FROM ( - -- Join to Time Spine Dataset + -- Constrain Output with WHERE + -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] SELECT - subq_24.ds AS metric_time__day - , subq_22.booking__is_instant AS booking__is_instant - , subq_22.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_24 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , booking__is_instant - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_17.ds AS metric_time__day - , subq_15.booking__is_instant AS booking__is_instant - , SUM(subq_15.bookings) AS bookings - FROM ***************************.mf_time_spine subq_17 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - ON - DATE_ADD('day', -5, subq_17.ds) = subq_15.metric_time__day - GROUP BY - subq_17.ds - , subq_15.booking__is_instant - ) subq_21 - ) subq_22 - ON - DATE_ADD('day', -2, subq_24.ds) = subq_22.metric_time__day - ) subq_25 - WHERE booking__is_instant -) subq_27 + subq_24.ds AS metric_time__day + , cm_7_cte.booking__is_instant AS booking__is_instant + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_24 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATE_ADD('day', -2, subq_24.ds) = cm_7_cte.metric_time__day + ) subq_25 + WHERE booking__is_instant + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql index 0694bbdbd..c7e77a9c0 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql @@ -2,65 +2,95 @@ test_name: test_nested_derived_metric_with_offset_multiple_input_metrics test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_14 +WITH cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_11_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month - , MAX(subq_30.booking_fees) AS booking_fees + metric_time__day + , booking_value * 0.05 AS booking_fees_start_of_month FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_10 SELECT - subq_23.ds AS metric_time__day - , subq_21.booking_fees_start_of_month AS booking_fees_start_of_month - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions - SELECT - metric_time__day - , booking_value * 0.05 AS booking_fees_start_of_month - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_20 - ) subq_21 - ON - DATE_TRUNC('month', subq_23.ds) = subq_21.metric_time__day - ) subq_24 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , booking_value + FROM cm_10_cte cm_10_cte + ) subq_20 +) + +, cm_12_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_value * 0.05 AS booking_fees + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , booking_value * 0.05 AS booking_fees + , booking_value + FROM cm_12_cte cm_12_cte + ) subq_29 +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_24.metric_time__day, cm_13_cte.metric_time__day) AS metric_time__day + , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month + , MAX(cm_13_cte.booking_fees) AS booking_fees FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_29 - ) subq_30 - ON - subq_24.metric_time__day = subq_30.metric_time__day - GROUP BY - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) -) subq_31 + subq_23.ds AS metric_time__day + , cm_11_cte.booking_fees_start_of_month AS booking_fees_start_of_month + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_11_cte cm_11_cte + ON + DATE_TRUNC('month', subq_23.ds) = cm_11_cte.metric_time__day + ) subq_24 + FULL OUTER JOIN + cm_13_cte cm_13_cte + ON + subq_24.metric_time__day = cm_13_cte.metric_time__day + GROUP BY + COALESCE(subq_24.metric_time__day, cm_13_cte.metric_time__day) + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , booking_fees_since_start_of_month AS booking_fees_since_start_of_month +FROM cm_14_cte cm_14_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql index b62cdb2dd..32085d34f 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql @@ -2,36 +2,60 @@ test_name: test_nested_fill_nulls_without_time_spine test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_8 - GROUP BY - metric_time__day - ) subq_9 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_8 + GROUP BY + metric_time__day + ) subq_9 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_10 -) subq_11 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_7_cte cm_7_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , nested_fill_nulls_without_time_spine AS nested_fill_nulls_without_time_spine +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql index aad7e04f4..3ae0a89f4 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql @@ -3,46 +3,59 @@ test_filename: test_derived_metric_rendering.py sql_engine: Trino --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_18.metric_time__day, subq_23.metric_time__day) AS metric_time__day - , MAX(subq_18.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_8_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine - FROM ( - -- Aggregate Measures - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_14 - GROUP BY - metric_time__day - ) subq_15 - ) subq_16 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_14 + GROUP BY + metric_time__day + ) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_8_cte cm_8_cte + ) subq_16 +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_9 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_9_cte cm_9_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_11_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -59,8 +72,16 @@ FULL OUTER JOIN ( ) subq_21 GROUP BY metric_time__day -) subq_23 +) + +SELECT + COALESCE(cm_10_cte.metric_time__day, cm_11_cte.metric_time__day) AS metric_time__day + , MAX(cm_10_cte.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine + , MAX(cm_11_cte.listings) AS listings +FROM cm_10_cte cm_10_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.metric_time__day = subq_23.metric_time__day + cm_10_cte.metric_time__day = cm_11_cte.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_23.metric_time__day) + COALESCE(cm_10_cte.metric_time__day, cm_11_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_filters__plan0_optimized.sql index d58e04b19..5b5660c6e 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_filters__plan0_optimized.sql @@ -4,67 +4,86 @@ docstring: Tests derived metric rendering for a nested derived metric with filters on the outer metric spec. sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - instant_lux_booking_value_rate AS instant_lux_booking_value_rate -FROM ( +-- Read From CTE For node_id=cm_15 +WITH cm_13_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings'] + -- Aggregate Measures -- Compute Metrics via Expressions SELECT - average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Combine Aggregated Outputs + -- Join Standard Outputs SELECT - MAX(subq_37.average_booking_value) AS average_booking_value - , MAX(subq_37.bookings) AS bookings - , MAX(subq_43.booking_value) AS booking_value + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_29.booking__is_instant AS booking__is_instant + , subq_29.bookings AS bookings + , subq_29.average_booking_value AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_29.booking__is_instant AS booking__is_instant - , subq_29.bookings AS bookings - , subq_29.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_29.listing = listings_latest_src_28000.listing_id - ) subq_33 - WHERE (listing__is_lux_latest) AND (booking__is_instant) - ) subq_37 - CROSS JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value',] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - SUM(booking_value) AS booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_39 - WHERE booking__is_instant - ) subq_43 + listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_29.listing = listings_latest_src_28000.listing_id + ) subq_33 + WHERE (listing__is_lux_latest) AND (booking__is_instant) +) + +, cm_12_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(booking_value) AS booking_value + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + is_instant AS booking__is_instant + , booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_39 + WHERE booking__is_instant +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(cm_13_cte.average_booking_value) AS average_booking_value + , MAX(cm_13_cte.bookings) AS bookings + , MAX(cm_12_cte.booking_value) AS booking_value + FROM cm_13_cte cm_13_cte + CROSS JOIN + cm_12_cte cm_12_cte ) subq_44 -) subq_45 +) + +, cm_15_cte AS ( + -- Compute Metrics via Expressions + SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate + FROM ( + -- Read From CTE For node_id=cm_14 + SELECT + instant_lux_booking_value_rate + FROM cm_14_cte cm_14_cte + ) subq_45 +) + +SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate +FROM cm_15_cte cm_15_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_offsets__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_offsets__plan0_optimized.sql index 02db30d70..a94f823b2 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_offsets__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_offsets__plan0_optimized.sql @@ -2,44 +2,63 @@ test_name: test_nested_offsets test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_22 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - ON - DATE_ADD('day', -5, subq_15.ds) = subq_13.metric_time__day - GROUP BY - subq_15.ds - ) subq_19 - ) subq_20 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 ON - DATE_ADD('day', -2, subq_22.ds) = subq_20.metric_time__day -) subq_23 + DATE_ADD('day', -5, subq_15.ds) = subq_13.metric_time__day + GROUP BY + subq_15.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_22.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_22 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATE_ADD('day', -2, subq_22.ds) = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_offsets_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_offsets_with_time_constraint__plan0_optimized.sql index d60807863..d814bbd14 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_offsets_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_offsets_with_time_constraint__plan0_optimized.sql @@ -2,50 +2,69 @@ test_name: test_nested_offsets_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.metric_time__day AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ( - -- Time Spine - SELECT - ds AS metric_time__day - FROM ***************************.mf_time_spine subq_22 - WHERE ds BETWEEN timestamp '2020-01-12' AND timestamp '2020-01-13' - ) subq_21 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 + ON + DATE_ADD('day', -5, subq_15.ds) = subq_13.metric_time__day + GROUP BY + subq_15.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 SELECT metric_time__day - , 2 * bookings AS bookings_offset_once + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_21.metric_time__day AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Time Spine SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - ON - DATE_ADD('day', -5, subq_15.ds) = subq_13.metric_time__day - GROUP BY - subq_15.ds - ) subq_19 - ) subq_20 - ON - DATE_ADD('day', -2, subq_21.metric_time__day) = subq_20.metric_time__day -) subq_23 + ds AS metric_time__day + FROM ***************************.mf_time_spine subq_22 + WHERE ds BETWEEN timestamp '2020-01-12' AND timestamp '2020-01-13' + ) subq_21 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATE_ADD('day', -2, subq_21.metric_time__day) = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_offsets_with_where_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_offsets_with_where_constraint__plan0_optimized.sql index eda2cec26..132b7e2f9 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_offsets_with_where_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_offsets_with_where_constraint__plan0_optimized.sql @@ -2,51 +2,70 @@ test_name: test_nested_offsets_with_where_constraint test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_16.ds AS metric_time__day + , SUM(subq_14.bookings) AS bookings + FROM ***************************.mf_time_spine subq_16 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_14 + ON + DATE_ADD('day', -5, subq_16.ds) = subq_14.metric_time__day + GROUP BY + subq_16.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_20 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__day - , bookings_offset_once + , 2 * bookings_offset_once AS bookings_offset_twice FROM ( - -- Join to Time Spine Dataset + -- Constrain Output with WHERE SELECT - subq_23.ds AS metric_time__day - , subq_21.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_16.ds AS metric_time__day - , SUM(subq_14.bookings) AS bookings - FROM ***************************.mf_time_spine subq_16 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_14 - ON - DATE_ADD('day', -5, subq_16.ds) = subq_14.metric_time__day - GROUP BY - subq_16.ds - ) subq_20 - ) subq_21 - ON - DATE_ADD('day', -2, subq_23.ds) = subq_21.metric_time__day - ) subq_24 - WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' -) subq_25 + subq_23.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATE_ADD('day', -2, subq_23.ds) = cm_7_cte.metric_time__day + ) subq_24 + WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' + ) subq_25 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index 600f39647..712d07aee 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,11 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with one granularity and filtered by a different one. sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__month'] -- Aggregate Measures @@ -38,4 +35,23 @@ FROM ( WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__month -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql index 88c1ed0b7..b6abc4b2c 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql @@ -4,13 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with multiple granularities. sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day', 'metric_time__month', 'metric_time__year'] -- Aggregate Measures @@ -35,4 +30,29 @@ FROM ( subq_11.ds , DATE_TRUNC('month', subq_11.ds) , DATE_TRUNC('year', subq_11.ds) -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql index 9f5c75d84..bd91dc722 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_to_grain_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.booking__ds__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + DATE_TRUNC('month', subq_22.ds) = subq_20.booking__ds__day GROUP BY - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index c2acb8801..d6d5669b7 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,63 +4,76 @@ docstring: Test a query where an offset window metric is queried with one granularity and filtered by a different one. sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) AS metric_time__month - , MAX(subq_24.booking_value) AS booking_value - , MAX(subq_30.bookers) AS bookers + metric_time__month + , SUM(booking_value) AS booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__month - , SUM(booking_value) AS booking_value - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_19.ds AS metric_time__day - , DATE_TRUNC('month', subq_19.ds) AS metric_time__month - , bookings_source_src_28000.booking_value AS booking_value - FROM ***************************.mf_time_spine subq_19 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 - ON - DATE_ADD('week', -1, subq_19.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) - ) subq_20 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_24 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookers', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_19.ds AS metric_time__day + , DATE_TRUNC('month', subq_19.ds) AS metric_time__month + , bookings_source_src_28000.booking_value AS booking_value + FROM ***************************.mf_time_spine subq_19 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATE_ADD('week', -1, subq_19.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + ) subq_20 + WHERE metric_time__day = '2020-01-01' + GROUP BY + metric_time__month +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__month + , COUNT(DISTINCT bookers) AS bookers + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__month - , COUNT(DISTINCT bookers) AS bookers - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , DATE_TRUNC('month', ds) AS metric_time__month - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_30 - ON - subq_24.metric_time__month = subq_30.metric_time__month + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('month', ds) AS metric_time__month + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + WHERE metric_time__day = '2020-01-01' GROUP BY - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) -) subq_31 + metric_time__month +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + GROUP BY + COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) + ) subq_31 +) + +SELECT + metric_time__month AS metric_time__month + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_window_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_window_metric_multiple_granularities__plan0_optimized.sql index 5111961e5..69fdbd874 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_window_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_window_metric_multiple_granularities__plan0_optimized.sql @@ -4,67 +4,82 @@ docstring: Test a query where an offset window metric is queried with multiple granularities. sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS metric_time__day + , DATE_TRUNC('month', subq_17.ds) AS metric_time__month + , DATE_TRUNC('year', subq_17.ds) AS metric_time__year + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATE_ADD('week', -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + GROUP BY + subq_17.ds + , DATE_TRUNC('month', subq_17.ds) + , DATE_TRUNC('year', subq_17.ds) +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('month', ds) AS metric_time__month + , DATE_TRUNC('year', ds) AS metric_time__year + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) + , DATE_TRUNC('month', ds) + , DATE_TRUNC('year', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) AS metric_time__month - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) AS metric_time__year - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + metric_time__day + , metric_time__month + , metric_time__year + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS metric_time__day - , DATE_TRUNC('month', subq_17.ds) AS metric_time__month - , DATE_TRUNC('year', subq_17.ds) AS metric_time__year - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - DATE_ADD('week', -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) AND ( + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + ) AND ( + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + ) GROUP BY - subq_17.ds - , DATE_TRUNC('month', subq_17.ds) - , DATE_TRUNC('year', subq_17.ds) - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , DATE_TRUNC('month', ds) AS metric_time__month - , DATE_TRUNC('year', ds) AS metric_time__year - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - , DATE_TRUNC('month', ds) - , DATE_TRUNC('year', ds) - ) subq_26 - ON - ( - subq_21.metric_time__day = subq_26.metric_time__day - ) AND ( - subq_21.metric_time__month = subq_26.metric_time__month - ) AND ( - subq_21.metric_time__year = subq_26.metric_time__year - ) - GROUP BY - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) -) subq_27 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + , COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) + , COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_window_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_window_with_agg_time_dim__plan0_optimized.sql index fe12cd0d6..6c4814524 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_window_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_window_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_ADD('day', -14, subq_22.ds) = subq_20.booking__ds__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + DATE_ADD('day', -14, subq_22.ds) = subq_20.booking__ds__day GROUP BY - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_time_offset_metric_with_time_constraint__plan0_optimized.sql index 0d06d2459..689b48430 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -33,4 +30,23 @@ FROM ( DATE_ADD('day', -5, subq_10.metric_time__day) = subq_9.metric_time__day GROUP BY subq_10.metric_time__day -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/BigQuery/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/BigQuery/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql index 576edf228..cf457d396 100644 --- a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/BigQuery/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/BigQuery/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql @@ -2,72 +2,96 @@ test_name: test_derived_fill_nulls_for_one_input_metric test_filename: test_fill_nulls_with_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_22.ds AS metric_time__day + , subq_20.bookings AS bookings + FROM ***************************.mf_time_spine subq_22 + LEFT OUTER JOIN ( + -- Aggregate Measures SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings AS bookings - FROM ***************************.mf_time_spine subq_22 - LEFT OUTER JOIN ( - -- Aggregate Measures + metric_time__day + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_19 - GROUP BY - metric_time__day - ) subq_20 - ON - subq_22.ds = subq_20.metric_time__day - ) subq_23 - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_19 + GROUP BY + metric_time__day + ) subq_20 + ON + subq_22.ds = subq_20.metric_time__day + ) subq_23 +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + ON + DATE_SUB(CAST(subq_28.ds AS DATETIME), INTERVAL 14 day) = subq_26.metric_time__day + GROUP BY + metric_time__day +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM ( + -- Read From CTE For node_id=cm_6 SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 + metric_time__day + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_24 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_32 ON - DATE_SUB(CAST(subq_28.ds AS DATETIME), INTERVAL 14 day) = subq_26.metric_time__day + subq_24.metric_time__day = subq_32.metric_time__day GROUP BY metric_time__day - ) subq_32 - ON - subq_24.metric_time__day = subq_32.metric_time__day - GROUP BY - metric_time__day -) subq_33 + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Databricks/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Databricks/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql index ba9e282ec..df20b2521 100644 --- a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Databricks/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Databricks/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql @@ -2,72 +2,96 @@ test_name: test_derived_fill_nulls_for_one_input_metric test_filename: test_fill_nulls_with_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_22.ds AS metric_time__day + , subq_20.bookings AS bookings + FROM ***************************.mf_time_spine subq_22 + LEFT OUTER JOIN ( + -- Aggregate Measures SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings AS bookings - FROM ***************************.mf_time_spine subq_22 - LEFT OUTER JOIN ( - -- Aggregate Measures + metric_time__day + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_19 - GROUP BY - metric_time__day - ) subq_20 - ON - subq_22.ds = subq_20.metric_time__day - ) subq_23 - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_19 + GROUP BY + metric_time__day + ) subq_20 + ON + subq_22.ds = subq_20.metric_time__day + ) subq_23 +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + ON + DATEADD(day, -14, subq_28.ds) = subq_26.metric_time__day + GROUP BY + subq_28.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset + FROM ( + -- Combine Aggregated Outputs SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM ( + -- Read From CTE For node_id=cm_6 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 + metric_time__day + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_24 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_32 ON - DATEADD(day, -14, subq_28.ds) = subq_26.metric_time__day + subq_24.metric_time__day = subq_32.metric_time__day GROUP BY - subq_28.ds - ) subq_32 - ON - subq_24.metric_time__day = subq_32.metric_time__day - GROUP BY - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) -) subq_33 + COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Postgres/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Postgres/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql index 44671f824..915e9dd91 100644 --- a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Postgres/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Postgres/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql @@ -2,72 +2,96 @@ test_name: test_derived_fill_nulls_for_one_input_metric test_filename: test_fill_nulls_with_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_22.ds AS metric_time__day + , subq_20.bookings AS bookings + FROM ***************************.mf_time_spine subq_22 + LEFT OUTER JOIN ( + -- Aggregate Measures SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings AS bookings - FROM ***************************.mf_time_spine subq_22 - LEFT OUTER JOIN ( - -- Aggregate Measures + metric_time__day + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_19 - GROUP BY - metric_time__day - ) subq_20 - ON - subq_22.ds = subq_20.metric_time__day - ) subq_23 - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_19 + GROUP BY + metric_time__day + ) subq_20 + ON + subq_22.ds = subq_20.metric_time__day + ) subq_23 +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + ON + subq_28.ds - MAKE_INTERVAL(days => 14) = subq_26.metric_time__day + GROUP BY + subq_28.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset + FROM ( + -- Combine Aggregated Outputs SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM ( + -- Read From CTE For node_id=cm_6 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 + metric_time__day + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_24 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_32 ON - subq_28.ds - MAKE_INTERVAL(days => 14) = subq_26.metric_time__day + subq_24.metric_time__day = subq_32.metric_time__day GROUP BY - subq_28.ds - ) subq_32 - ON - subq_24.metric_time__day = subq_32.metric_time__day - GROUP BY - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) -) subq_33 + COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Redshift/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Redshift/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql index 25e33fd18..c78ebbf2d 100644 --- a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Redshift/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Redshift/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql @@ -2,72 +2,96 @@ test_name: test_derived_fill_nulls_for_one_input_metric test_filename: test_fill_nulls_with_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_22.ds AS metric_time__day + , subq_20.bookings AS bookings + FROM ***************************.mf_time_spine subq_22 + LEFT OUTER JOIN ( + -- Aggregate Measures SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings AS bookings - FROM ***************************.mf_time_spine subq_22 - LEFT OUTER JOIN ( - -- Aggregate Measures + metric_time__day + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_19 - GROUP BY - metric_time__day - ) subq_20 - ON - subq_22.ds = subq_20.metric_time__day - ) subq_23 - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_19 + GROUP BY + metric_time__day + ) subq_20 + ON + subq_22.ds = subq_20.metric_time__day + ) subq_23 +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + ON + DATEADD(day, -14, subq_28.ds) = subq_26.metric_time__day + GROUP BY + subq_28.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset + FROM ( + -- Combine Aggregated Outputs SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM ( + -- Read From CTE For node_id=cm_6 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 + metric_time__day + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_24 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_32 ON - DATEADD(day, -14, subq_28.ds) = subq_26.metric_time__day + subq_24.metric_time__day = subq_32.metric_time__day GROUP BY - subq_28.ds - ) subq_32 - ON - subq_24.metric_time__day = subq_32.metric_time__day - GROUP BY - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) -) subq_33 + COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Snowflake/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Snowflake/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql index 34a698d3f..8bbeeb485 100644 --- a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Snowflake/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Snowflake/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql @@ -2,72 +2,96 @@ test_name: test_derived_fill_nulls_for_one_input_metric test_filename: test_fill_nulls_with_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_22.ds AS metric_time__day + , subq_20.bookings AS bookings + FROM ***************************.mf_time_spine subq_22 + LEFT OUTER JOIN ( + -- Aggregate Measures SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings AS bookings - FROM ***************************.mf_time_spine subq_22 - LEFT OUTER JOIN ( - -- Aggregate Measures + metric_time__day + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_19 - GROUP BY - metric_time__day - ) subq_20 - ON - subq_22.ds = subq_20.metric_time__day - ) subq_23 - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_19 + GROUP BY + metric_time__day + ) subq_20 + ON + subq_22.ds = subq_20.metric_time__day + ) subq_23 +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + ON + DATEADD(day, -14, subq_28.ds) = subq_26.metric_time__day + GROUP BY + subq_28.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset + FROM ( + -- Combine Aggregated Outputs SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM ( + -- Read From CTE For node_id=cm_6 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 + metric_time__day + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_24 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_32 ON - DATEADD(day, -14, subq_28.ds) = subq_26.metric_time__day + subq_24.metric_time__day = subq_32.metric_time__day GROUP BY - subq_28.ds - ) subq_32 - ON - subq_24.metric_time__day = subq_32.metric_time__day - GROUP BY - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) -) subq_33 + COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Trino/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Trino/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql index 45f332582..e5ac2806f 100644 --- a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Trino/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Trino/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql @@ -2,72 +2,96 @@ test_name: test_derived_fill_nulls_for_one_input_metric test_filename: test_fill_nulls_with_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_22.ds AS metric_time__day + , subq_20.bookings AS bookings + FROM ***************************.mf_time_spine subq_22 + LEFT OUTER JOIN ( + -- Aggregate Measures SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings AS bookings - FROM ***************************.mf_time_spine subq_22 - LEFT OUTER JOIN ( - -- Aggregate Measures + metric_time__day + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_19 - GROUP BY - metric_time__day - ) subq_20 - ON - subq_22.ds = subq_20.metric_time__day - ) subq_23 - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_19 + GROUP BY + metric_time__day + ) subq_20 + ON + subq_22.ds = subq_20.metric_time__day + ) subq_23 +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + ON + DATE_ADD('day', -14, subq_28.ds) = subq_26.metric_time__day + GROUP BY + subq_28.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset + FROM ( + -- Combine Aggregated Outputs SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM ( + -- Read From CTE For node_id=cm_6 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 + metric_time__day + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_24 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_32 ON - DATE_ADD('day', -14, subq_28.ds) = subq_26.metric_time__day + subq_24.metric_time__day = subq_32.metric_time__day GROUP BY - subq_28.ds - ) subq_32 - ON - subq_24.metric_time__day = subq_32.metric_time__day - GROUP BY - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) -) subq_33 + COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_with_date_part__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_with_date_part__plan0_optimized.sql index 6aad32a7f..caea2a8e8 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_with_date_part__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_with_date_part__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_date_part test_filename: test_granularity_date_part_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__extract_dow - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) AS metric_time__extract_dow - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__extract_dow + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__extract_dow - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - SELECT - IF(EXTRACT(dayofweek FROM ds) = 1, 7, EXTRACT(dayofweek FROM ds) - 1) AS metric_time__extract_dow - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__extract_dow - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - IF(EXTRACT(dayofweek FROM subq_22.ds) = 1, 7, EXTRACT(dayofweek FROM subq_22.ds) - 1) AS metric_time__extract_dow - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_SUB(CAST(subq_22.ds AS DATETIME), INTERVAL 14 day) = subq_20.metric_time__day - GROUP BY - metric_time__extract_dow - ) subq_26 + IF(EXTRACT(dayofweek FROM ds) = 1, 7, EXTRACT(dayofweek FROM ds) - 1) AS metric_time__extract_dow + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__extract_dow +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + IF(EXTRACT(dayofweek FROM subq_22.ds) = 1, 7, EXTRACT(dayofweek FROM subq_22.ds) - 1) AS metric_time__extract_dow + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__extract_dow = subq_26.metric_time__extract_dow + DATE_SUB(CAST(subq_22.ds AS DATETIME), INTERVAL 14 day) = subq_20.metric_time__day GROUP BY metric_time__extract_dow -) subq_27 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__extract_dow + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__extract_dow, cm_7_cte.metric_time__extract_dow) AS metric_time__extract_dow + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__extract_dow = cm_7_cte.metric_time__extract_dow + GROUP BY + metric_time__extract_dow + ) subq_27 +) + +SELECT + metric_time__extract_dow AS metric_time__extract_dow + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/BigQuery/test_subdaily_offset_to_grain_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/BigQuery/test_subdaily_offset_to_grain_metric__plan0_optimized.sql index 5a9219415..e1335a1ba 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/BigQuery/test_subdaily_offset_to_grain_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/BigQuery/test_subdaily_offset_to_grain_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_to_grain_metric test_filename: test_granularity_date_part_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_grain_to_date_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATETIME_TRUNC(subq_11.ts, hour) = subq_9.metric_time__hour GROUP BY metric_time__hour -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_grain_to_date_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_grain_to_date_metric AS subdaily_offset_grain_to_date_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/BigQuery/test_subdaily_offset_window_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/BigQuery/test_subdaily_offset_window_metric__plan0_optimized.sql index 7997b120d..2dcbef322 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/BigQuery/test_subdaily_offset_window_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/BigQuery/test_subdaily_offset_window_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_window_metric test_filename: test_granularity_date_part_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_window_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATE_SUB(CAST(subq_11.ts AS DATETIME), INTERVAL 1 hour) = subq_9.metric_time__hour GROUP BY metric_time__hour -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_window_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_window_metric AS subdaily_offset_window_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Databricks/test_offset_window_with_date_part__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Databricks/test_offset_window_with_date_part__plan0_optimized.sql index fcbbcc1ba..262cb6056 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Databricks/test_offset_window_with_date_part__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Databricks/test_offset_window_with_date_part__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_date_part test_filename: test_granularity_date_part_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__extract_dow - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) AS metric_time__extract_dow - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__extract_dow + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__extract_dow - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - SELECT - EXTRACT(DAYOFWEEK_ISO FROM ds) AS metric_time__extract_dow - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__extract_dow - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - EXTRACT(DAYOFWEEK_ISO FROM subq_22.ds) AS metric_time__extract_dow - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day - GROUP BY - EXTRACT(DAYOFWEEK_ISO FROM subq_22.ds) - ) subq_26 + EXTRACT(DAYOFWEEK_ISO FROM ds) AS metric_time__extract_dow + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__extract_dow +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + EXTRACT(DAYOFWEEK_ISO FROM subq_22.ds) AS metric_time__extract_dow + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__extract_dow = subq_26.metric_time__extract_dow + DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) -) subq_27 + EXTRACT(DAYOFWEEK_ISO FROM subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__extract_dow + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__extract_dow, cm_7_cte.metric_time__extract_dow) AS metric_time__extract_dow + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__extract_dow = cm_7_cte.metric_time__extract_dow + GROUP BY + COALESCE(cm_6_cte.metric_time__extract_dow, cm_7_cte.metric_time__extract_dow) + ) subq_27 +) + +SELECT + metric_time__extract_dow AS metric_time__extract_dow + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Databricks/test_subdaily_offset_to_grain_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Databricks/test_subdaily_offset_to_grain_metric__plan0_optimized.sql index a2be431b3..101fdc286 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Databricks/test_subdaily_offset_to_grain_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Databricks/test_subdaily_offset_to_grain_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_to_grain_metric test_filename: test_granularity_date_part_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_grain_to_date_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATE_TRUNC('hour', subq_11.ts) = subq_9.metric_time__hour GROUP BY subq_11.ts -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_grain_to_date_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_grain_to_date_metric AS subdaily_offset_grain_to_date_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Databricks/test_subdaily_offset_window_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Databricks/test_subdaily_offset_window_metric__plan0_optimized.sql index 33ac7b7e6..19a4ec44f 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Databricks/test_subdaily_offset_window_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Databricks/test_subdaily_offset_window_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_window_metric test_filename: test_granularity_date_part_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_window_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATEADD(hour, -1, subq_11.ts) = subq_9.metric_time__hour GROUP BY subq_11.ts -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_window_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_window_metric AS subdaily_offset_window_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Postgres/test_offset_window_with_date_part__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Postgres/test_offset_window_with_date_part__plan0_optimized.sql index 5e0bf2bbc..193e9d00d 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Postgres/test_offset_window_with_date_part__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Postgres/test_offset_window_with_date_part__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_date_part test_filename: test_granularity_date_part_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__extract_dow - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) AS metric_time__extract_dow - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__extract_dow + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__extract_dow - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - SELECT - EXTRACT(isodow FROM ds) AS metric_time__extract_dow - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__extract_dow - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - EXTRACT(isodow FROM subq_22.ds) AS metric_time__extract_dow - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - subq_22.ds - MAKE_INTERVAL(days => 14) = subq_20.metric_time__day - GROUP BY - EXTRACT(isodow FROM subq_22.ds) - ) subq_26 + EXTRACT(isodow FROM ds) AS metric_time__extract_dow + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__extract_dow +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + EXTRACT(isodow FROM subq_22.ds) AS metric_time__extract_dow + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__extract_dow = subq_26.metric_time__extract_dow + subq_22.ds - MAKE_INTERVAL(days => 14) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) -) subq_27 + EXTRACT(isodow FROM subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__extract_dow + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__extract_dow, cm_7_cte.metric_time__extract_dow) AS metric_time__extract_dow + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__extract_dow = cm_7_cte.metric_time__extract_dow + GROUP BY + COALESCE(cm_6_cte.metric_time__extract_dow, cm_7_cte.metric_time__extract_dow) + ) subq_27 +) + +SELECT + metric_time__extract_dow AS metric_time__extract_dow + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Postgres/test_subdaily_offset_to_grain_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Postgres/test_subdaily_offset_to_grain_metric__plan0_optimized.sql index 4ebee052c..c707d7989 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Postgres/test_subdaily_offset_to_grain_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Postgres/test_subdaily_offset_to_grain_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_to_grain_metric test_filename: test_granularity_date_part_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_grain_to_date_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATE_TRUNC('hour', subq_11.ts) = subq_9.metric_time__hour GROUP BY subq_11.ts -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_grain_to_date_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_grain_to_date_metric AS subdaily_offset_grain_to_date_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Postgres/test_subdaily_offset_window_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Postgres/test_subdaily_offset_window_metric__plan0_optimized.sql index 41dd8e327..7338a4b1c 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Postgres/test_subdaily_offset_window_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Postgres/test_subdaily_offset_window_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_window_metric test_filename: test_granularity_date_part_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_window_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( subq_11.ts - MAKE_INTERVAL(hours => 1) = subq_9.metric_time__hour GROUP BY subq_11.ts -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_window_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_window_metric AS subdaily_offset_window_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Redshift/test_offset_window_with_date_part__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Redshift/test_offset_window_with_date_part__plan0_optimized.sql index c2b638224..f19c76ea7 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Redshift/test_offset_window_with_date_part__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Redshift/test_offset_window_with_date_part__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_date_part test_filename: test_granularity_date_part_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__extract_dow - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) AS metric_time__extract_dow - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__extract_dow + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__extract_dow - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - SELECT - CASE WHEN EXTRACT(dow FROM ds) = 0 THEN EXTRACT(dow FROM ds) + 7 ELSE EXTRACT(dow FROM ds) END AS metric_time__extract_dow - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__extract_dow - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - CASE WHEN EXTRACT(dow FROM subq_22.ds) = 0 THEN EXTRACT(dow FROM subq_22.ds) + 7 ELSE EXTRACT(dow FROM subq_22.ds) END AS metric_time__extract_dow - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day - GROUP BY - CASE WHEN EXTRACT(dow FROM subq_22.ds) = 0 THEN EXTRACT(dow FROM subq_22.ds) + 7 ELSE EXTRACT(dow FROM subq_22.ds) END - ) subq_26 + CASE WHEN EXTRACT(dow FROM ds) = 0 THEN EXTRACT(dow FROM ds) + 7 ELSE EXTRACT(dow FROM ds) END AS metric_time__extract_dow + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__extract_dow +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + CASE WHEN EXTRACT(dow FROM subq_22.ds) = 0 THEN EXTRACT(dow FROM subq_22.ds) + 7 ELSE EXTRACT(dow FROM subq_22.ds) END AS metric_time__extract_dow + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__extract_dow = subq_26.metric_time__extract_dow + DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) -) subq_27 + CASE WHEN EXTRACT(dow FROM subq_22.ds) = 0 THEN EXTRACT(dow FROM subq_22.ds) + 7 ELSE EXTRACT(dow FROM subq_22.ds) END +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__extract_dow + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__extract_dow, cm_7_cte.metric_time__extract_dow) AS metric_time__extract_dow + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__extract_dow = cm_7_cte.metric_time__extract_dow + GROUP BY + COALESCE(cm_6_cte.metric_time__extract_dow, cm_7_cte.metric_time__extract_dow) + ) subq_27 +) + +SELECT + metric_time__extract_dow AS metric_time__extract_dow + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Redshift/test_subdaily_offset_to_grain_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Redshift/test_subdaily_offset_to_grain_metric__plan0_optimized.sql index 7b1ea188a..609d525d1 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Redshift/test_subdaily_offset_to_grain_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Redshift/test_subdaily_offset_to_grain_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_to_grain_metric test_filename: test_granularity_date_part_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_grain_to_date_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATE_TRUNC('hour', subq_11.ts) = subq_9.metric_time__hour GROUP BY subq_11.ts -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_grain_to_date_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_grain_to_date_metric AS subdaily_offset_grain_to_date_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Redshift/test_subdaily_offset_window_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Redshift/test_subdaily_offset_window_metric__plan0_optimized.sql index eeb440b35..4bd81e4c0 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Redshift/test_subdaily_offset_window_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Redshift/test_subdaily_offset_window_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_window_metric test_filename: test_granularity_date_part_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_window_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATEADD(hour, -1, subq_11.ts) = subq_9.metric_time__hour GROUP BY subq_11.ts -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_window_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_window_metric AS subdaily_offset_window_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_with_date_part__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_with_date_part__plan0_optimized.sql index 656edaf9f..709b77c99 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_with_date_part__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_with_date_part__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_date_part test_filename: test_granularity_date_part_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__extract_dow - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) AS metric_time__extract_dow - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__extract_dow + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__extract_dow - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - SELECT - EXTRACT(dayofweekiso FROM ds) AS metric_time__extract_dow - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__extract_dow - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - EXTRACT(dayofweekiso FROM subq_22.ds) AS metric_time__extract_dow - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day - GROUP BY - EXTRACT(dayofweekiso FROM subq_22.ds) - ) subq_26 + EXTRACT(dayofweekiso FROM ds) AS metric_time__extract_dow + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__extract_dow +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + EXTRACT(dayofweekiso FROM subq_22.ds) AS metric_time__extract_dow + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__extract_dow = subq_26.metric_time__extract_dow + DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) -) subq_27 + EXTRACT(dayofweekiso FROM subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__extract_dow + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__extract_dow, cm_7_cte.metric_time__extract_dow) AS metric_time__extract_dow + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__extract_dow = cm_7_cte.metric_time__extract_dow + GROUP BY + COALESCE(cm_6_cte.metric_time__extract_dow, cm_7_cte.metric_time__extract_dow) + ) subq_27 +) + +SELECT + metric_time__extract_dow AS metric_time__extract_dow + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Snowflake/test_subdaily_offset_to_grain_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Snowflake/test_subdaily_offset_to_grain_metric__plan0_optimized.sql index 0ac8b2525..6ed276d78 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Snowflake/test_subdaily_offset_to_grain_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Snowflake/test_subdaily_offset_to_grain_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_to_grain_metric test_filename: test_granularity_date_part_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_grain_to_date_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATE_TRUNC('hour', subq_11.ts) = subq_9.metric_time__hour GROUP BY subq_11.ts -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_grain_to_date_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_grain_to_date_metric AS subdaily_offset_grain_to_date_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Snowflake/test_subdaily_offset_window_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Snowflake/test_subdaily_offset_window_metric__plan0_optimized.sql index 0ee8b61db..4a52434fb 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Snowflake/test_subdaily_offset_window_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Snowflake/test_subdaily_offset_window_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_window_metric test_filename: test_granularity_date_part_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_window_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATEADD(hour, -1, subq_11.ts) = subq_9.metric_time__hour GROUP BY subq_11.ts -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_window_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_window_metric AS subdaily_offset_window_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Trino/test_offset_window_with_date_part__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Trino/test_offset_window_with_date_part__plan0_optimized.sql index 4def88a76..1eca1185c 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Trino/test_offset_window_with_date_part__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Trino/test_offset_window_with_date_part__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_date_part test_filename: test_granularity_date_part_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__extract_dow - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) AS metric_time__extract_dow - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__extract_dow + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__extract_dow - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - SELECT - EXTRACT(DAY_OF_WEEK FROM ds) AS metric_time__extract_dow - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__extract_dow - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - EXTRACT(DAY_OF_WEEK FROM subq_22.ds) AS metric_time__extract_dow - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_ADD('day', -14, subq_22.ds) = subq_20.metric_time__day - GROUP BY - EXTRACT(DAY_OF_WEEK FROM subq_22.ds) - ) subq_26 + EXTRACT(DAY_OF_WEEK FROM ds) AS metric_time__extract_dow + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__extract_dow +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + EXTRACT(DAY_OF_WEEK FROM subq_22.ds) AS metric_time__extract_dow + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__extract_dow = subq_26.metric_time__extract_dow + DATE_ADD('day', -14, subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) -) subq_27 + EXTRACT(DAY_OF_WEEK FROM subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__extract_dow + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__extract_dow, cm_7_cte.metric_time__extract_dow) AS metric_time__extract_dow + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__extract_dow = cm_7_cte.metric_time__extract_dow + GROUP BY + COALESCE(cm_6_cte.metric_time__extract_dow, cm_7_cte.metric_time__extract_dow) + ) subq_27 +) + +SELECT + metric_time__extract_dow AS metric_time__extract_dow + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Trino/test_subdaily_offset_to_grain_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Trino/test_subdaily_offset_to_grain_metric__plan0_optimized.sql index 6eca0ddc3..64979d6fd 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Trino/test_subdaily_offset_to_grain_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Trino/test_subdaily_offset_to_grain_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_to_grain_metric test_filename: test_granularity_date_part_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_grain_to_date_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATE_TRUNC('hour', subq_11.ts) = subq_9.metric_time__hour GROUP BY subq_11.ts -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_grain_to_date_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_grain_to_date_metric AS subdaily_offset_grain_to_date_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Trino/test_subdaily_offset_window_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Trino/test_subdaily_offset_window_metric__plan0_optimized.sql index f51d27ebe..4af655515 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Trino/test_subdaily_offset_window_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Trino/test_subdaily_offset_window_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_window_metric test_filename: test_granularity_date_part_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_window_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATE_ADD('hour', -1, subq_11.ts) = subq_9.metric_time__hour GROUP BY subq_11.ts -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_window_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_window_metric AS subdaily_offset_window_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql index 5166802d9..e68b86149 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: BigQuery --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'guest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + guest_id AS guest + , SUM(booking_value) AS guest__booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + guest +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.guest__booking_value AS guest__booking_value - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - guest_id AS guest - , guest_id AS bookers + cm_3_cte.guest__booking_value AS guest__booking_value + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'guest'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['guest', 'guest__booking_value'] - SELECT - guest_id AS guest - , SUM(booking_value) AS guest__booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - guest - ) subq_19 - ON - subq_13.guest = subq_19.guest -) subq_20 -WHERE guest__booking_value > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.guest_id = cm_3_cte.guest + ) subq_20 + WHERE guest__booking_value > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_filter_with_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_filter_with_conversion_metric__plan0_optimized.sql index 72abea640..d1208e5b9 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_filter_with_conversion_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_filter_with_conversion_metric__plan0_optimized.sql @@ -2,26 +2,13 @@ test_name: test_filter_with_conversion_metric test_filename: test_metric_filter_rendering.py sql_engine: BigQuery --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Compute Metrics via Expressions SELECT - CAST(subq_39.buys AS FLOAT64) / CAST(NULLIF(subq_39.visits, 0) AS FLOAT64) AS user__visit_buy_conversion_rate - , subq_24.listings AS listings + subq_39.user + , CAST(buys AS FLOAT64) / CAST(NULLIF(visits, 0) AS FLOAT64) AS user__visit_buy_conversion_rate FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_24 - LEFT OUTER JOIN ( -- Combine Aggregated Outputs SELECT COALESCE(subq_28.user, subq_38.user) AS user @@ -116,7 +103,36 @@ FROM ( GROUP BY user ) subq_39 - ON - subq_24.user = subq_39.user -) subq_42 -WHERE user__visit_buy_conversion_rate > 2 +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.user__visit_buy_conversion_rate AS user__visit_buy_conversion_rate + , subq_24.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_24 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_24.user = cm_3_cte.user + ) subq_42 + WHERE user__visit_buy_conversion_rate > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_group_by_has_local_entity_prefix__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_group_by_has_local_entity_prefix__plan0_optimized.sql index 74a410722..75c501cb9 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_group_by_has_local_entity_prefix__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_group_by_has_local_entity_prefix__plan0_optimized.sql @@ -2,43 +2,52 @@ test_name: test_group_by_has_local_entity_prefix test_filename: test_metric_filter_rendering.py sql_engine: BigQuery --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['average_booking_value', 'listing__user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listings_latest_src_28000.user_id AS listing__user + , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id + GROUP BY + listing__user +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_27.listing__user__average_booking_value AS user__listing__user__average_booking_value - , subq_17.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_17 - LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['average_booking_value', 'listing__user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing__user', 'listing__user__average_booking_value'] SELECT - listings_latest_src_28000.user_id AS listing__user - , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + cm_3_cte.listing__user__average_booking_value AS user__listing__user__average_booking_value + , subq_17.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_17 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + cm_3_cte cm_3_cte ON - bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id - GROUP BY - listing__user - ) subq_27 - ON - subq_17.user = subq_27.listing__user -) subq_28 -WHERE user__listing__user__average_booking_value > 1 + subq_17.user = cm_3_cte.listing__user + ) subq_28 + WHERE user__listing__user__average_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_inner_query_multi_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_inner_query_multi_hop__plan0_optimized.sql index 4871deb40..01d43bd8f 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_inner_query_multi_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_inner_query_multi_hop__plan0_optimized.sql @@ -4,51 +4,60 @@ docstring: Tests rendering for a metric filter using a two-hop join in the inner query. sql_engine: BigQuery --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_35.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 + subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id + , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count + FROM ***************************.account_month_txns account_month_txns_src_22000 LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['account_id__customer_id__customer_third_hop_id', 'account_id__customer_id__customer_third_hop_id__txn_count'] + -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] SELECT - subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id - , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count - FROM ***************************.account_month_txns account_month_txns_src_22000 - LEFT OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] - SELECT - DATETIME_TRUNC(bridge_table_src_22000.ds_partitioned, day) AS ds_partitioned__day - , bridge_table_src_22000.account_id AS account_id - , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id - FROM ***************************.bridge_table bridge_table_src_22000 - LEFT OUTER JOIN - ***************************.customer_other_data customer_other_data_src_22000 - ON - bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id - ) subq_30 + DATETIME_TRUNC(bridge_table_src_22000.ds_partitioned, day) AS ds_partitioned__day + , bridge_table_src_22000.account_id AS account_id + , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id + FROM ***************************.bridge_table bridge_table_src_22000 + LEFT OUTER JOIN + ***************************.customer_other_data customer_other_data_src_22000 ON - ( - account_month_txns_src_22000.account_id = subq_30.account_id - ) AND ( - DATETIME_TRUNC(account_month_txns_src_22000.ds_partitioned, day) = subq_30.ds_partitioned__day - ) - GROUP BY - account_id__customer_id__customer_third_hop_id - ) subq_35 + bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id + ) subq_30 ON - third_hop_table_src_22000.customer_third_hop_id = subq_35.account_id__customer_id__customer_third_hop_id -) subq_36 -WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 + ( + account_month_txns_src_22000.account_id = subq_30.account_id + ) AND ( + DATETIME_TRUNC(account_month_txns_src_22000.ds_partitioned, day) = subq_30.ds_partitioned__day + ) + GROUP BY + account_id__customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.account_id__customer_id__customer_third_hop_id + ) subq_36 + WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_inner_query_single_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_inner_query_single_hop__plan0_optimized.sql index e5c386cf6..93abe72de 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_inner_query_single_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_inner_query_single_hop__plan0_optimized.sql @@ -4,41 +4,50 @@ docstring: Tests rendering for a metric filter using a one-hop join in the inner query. sql_engine: BigQuery --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + customer_id__customer_third_hop_id + , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers + FROM ( + -- Read Elements From Semantic Model 'customer_other_data' + -- Metric Time Dimension 'acquired_ds' + SELECT + customer_third_hop_id AS customer_id__customer_third_hop_id + , country AS customer_id__country + , 1 AS customers_with_other_data + FROM ***************************.customer_other_data customer_other_data_src_22000 + ) subq_16 + WHERE customer_id__country = 'paraguay' + GROUP BY + customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['customer_id__customer_third_hop_id', 'customer_id__customer_third_hop_id__paraguayan_customers'] + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs SELECT - customer_id__customer_third_hop_id - , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers - FROM ( - -- Read Elements From Semantic Model 'customer_other_data' - -- Metric Time Dimension 'acquired_ds' - SELECT - customer_third_hop_id AS customer_id__customer_third_hop_id - , country AS customer_id__country - , 1 AS customers_with_other_data - FROM ***************************.customer_other_data customer_other_data_src_22000 - ) subq_16 - WHERE customer_id__country = 'paraguay' - GROUP BY - customer_id__customer_third_hop_id - ) subq_21 - ON - third_hop_table_src_22000.customer_third_hop_id = subq_21.customer_id__customer_third_hop_id -) subq_22 -WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 + cm_3_cte.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.customer_id__customer_third_hop_id + ) subq_22 + WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_metric_filtered_by_itself__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_metric_filtered_by_itself__plan0_optimized.sql index eb9387e67..157bc7134 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_metric_filtered_by_itself__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_metric_filtered_by_itself__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query for a metric that filters by the same metric. sql_engine: BigQuery --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookers AS listing__bookers - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - listing_id AS listing - , guest_id AS bookers + cm_3_cte.listing__bookers AS listing__bookers + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookers > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.listing_id = cm_3_cte.listing + ) subq_20 + WHERE listing__bookers > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_metric_with_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_metric_with_metric_in_where_filter__plan0_optimized.sql index 5cb94bbb5..86cb32388 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_metric_with_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_metric_with_metric_in_where_filter__plan0_optimized.sql @@ -4,50 +4,60 @@ docstring: Tests a query with a metric in the metric-level where filter. sql_engine: BigQuery --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings', 'metric_time__day'] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - metric_time__day - , SUM(listings) AS active_listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.metric_time__day AS metric_time__day - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT - DATETIME_TRUNC(created_at, day) AS metric_time__day - , listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(listings) AS active_listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.metric_time__day AS metric_time__day + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT - listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 -GROUP BY - metric_time__day + DATETIME_TRUNC(created_at, day) AS metric_time__day + , listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 + GROUP BY + metric_time__day +) + +SELECT + metric_time__day AS metric_time__day + , active_listings AS active_listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql index 4fe187895..fc3d11be2 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql @@ -6,40 +6,49 @@ docstring: Note this cumulative metric has no window / grain to date. sql_engine: BigQuery --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'revenue' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['txn_revenue', 'user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + user_id AS user + , SUM(revenue) AS user__revenue_all_time + FROM ***************************.fct_revenue revenue_src_28000 + GROUP BY + user +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.user__revenue_all_time AS user__revenue_all_time - , subq_13.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'revenue' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['txn_revenue', 'user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['user', 'user__revenue_all_time'] - SELECT - user_id AS user - , SUM(revenue) AS user__revenue_all_time - FROM ***************************.fct_revenue revenue_src_28000 - GROUP BY - user - ) subq_19 - ON - subq_13.user = subq_19.user -) subq_20 -WHERE user__revenue_all_time > 1 + cm_3_cte.user__revenue_all_time AS user__revenue_all_time + , subq_13.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.user = cm_3_cte.user + ) subq_20 + WHERE user__revenue_all_time > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql index f5815e861..8b11a76c7 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql @@ -4,75 +4,89 @@ docstring: Tests a query with a derived metric in the query-level where filter. sql_engine: BigQuery --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing +) + +, cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_33.listing__views_times_booking_value AS listing__views_times_booking_value - , subq_20.listings AS listings + listing + , SUM(views) AS views FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'views_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['views', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__views_times_booking_value'] + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_28 + GROUP BY + listing +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing + , booking_value * views AS listing__views_times_booking_value + FROM ( + -- Combine Aggregated Outputs SELECT + COALESCE(cm_5_cte.listing, cm_6_cte.listing) AS listing + , MAX(cm_5_cte.booking_value) AS booking_value + , MAX(cm_6_cte.views) AS views + FROM cm_5_cte cm_5_cte + FULL OUTER JOIN + cm_6_cte cm_6_cte + ON + cm_5_cte.listing = cm_6_cte.listing + GROUP BY listing - , booking_value * views AS listing__views_times_booking_value + ) subq_31 +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_7_cte.listing__views_times_booking_value AS listing__views_times_booking_value + , subq_20.listings AS listings FROM ( - -- Combine Aggregated Outputs + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' SELECT - COALESCE(subq_25.listing, subq_30.listing) AS listing - , MAX(subq_25.booking_value) AS booking_value - , MAX(subq_30.views) AS views - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing_id AS listing - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing - ) subq_25 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing - , SUM(views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['views', 'listing'] - SELECT - listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_28 - GROUP BY - listing - ) subq_30 - ON - subq_25.listing = subq_30.listing - GROUP BY - listing - ) subq_31 - ) subq_33 - ON - subq_20.listing = subq_33.listing -) subq_34 -WHERE listing__views_times_booking_value > 1 + listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_7_cte cm_7_cte + ON + subq_20.listing = cm_7_cte.listing + ) subq_34 + WHERE listing__views_times_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql index 4917aea69..e61eb998d 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql @@ -4,62 +4,73 @@ docstring: Tests a query with 2 simple metrics in the query-level where filter. sql_engine: BigQuery --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_6 +WITH cm_4_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_25.listing__bookings AS listing__bookings - , subq_31.listing__bookers AS listing__bookers - , subq_19.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_19 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_22 + GROUP BY + listing +) + +, cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing +) + +, cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_4_cte.listing__bookings AS listing__bookings + , cm_5_cte.listing__bookers AS listing__bookers + , subq_19.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_22 - GROUP BY - listing - ) subq_25 - ON - subq_19.listing = subq_25.listing - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing - ) subq_31 - ON - subq_19.listing = subq_31.listing -) subq_32 -WHERE listing__bookings > 2 AND listing__bookers > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_19 + LEFT OUTER JOIN + cm_4_cte cm_4_cte + ON + subq_19.listing = cm_4_cte.listing + LEFT OUTER JOIN + cm_5_cte cm_5_cte + ON + subq_19.listing = cm_5_cte.listing + ) subq_32 + WHERE listing__bookings > 2 AND listing__bookers > 1 +) + +SELECT + listings AS listings +FROM cm_6_cte cm_6_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql index f4d381151..0ffa4c9da 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql @@ -4,46 +4,65 @@ docstring: Tests a query with a ratio metric in the query-level where filter. sql_engine: BigQuery --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_9 +WITH cm_7_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - CAST(subq_25.bookings AS FLOAT64) / CAST(NULLIF(subq_25.bookers, 0) AS FLOAT64) AS listing__bookings_per_booker - , subq_20.listings AS listings + listing + , SUM(bookings) AS bookings + , COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + , 1 AS bookings + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_23 + GROUP BY + listing +) + +, cm_8_cte AS ( + -- Read From CTE For node_id=cm_7 + -- Compute Metrics via Expressions + SELECT + listing + , CAST(bookings AS FLOAT64) / CAST(NULLIF(bookers, 0) AS FLOAT64) AS listing__bookings_per_booker + FROM cm_7_cte cm_7_cte +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS bookings - , COUNT(DISTINCT bookers) AS bookers + cm_8_cte.listing__bookings_per_booker AS listing__bookings_per_booker + , subq_20.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - GROUP BY - listing - ) subq_25 - ON - subq_20.listing = subq_25.listing -) subq_28 -WHERE listing__bookings_per_booker > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_8_cte cm_8_cte + ON + subq_20.listing = cm_8_cte.listing + ) subq_28 + WHERE listing__bookings_per_booker > 1 +) + +SELECT + listings AS listings +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql index 1315f9edf..aefe125f0 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql @@ -4,45 +4,54 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: BigQuery --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql index 66dbc3dce..baf3eafdd 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: Databricks --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'guest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + guest_id AS guest + , SUM(booking_value) AS guest__booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + guest_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.guest__booking_value AS guest__booking_value - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - guest_id AS guest - , guest_id AS bookers + cm_3_cte.guest__booking_value AS guest__booking_value + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'guest'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['guest', 'guest__booking_value'] - SELECT - guest_id AS guest - , SUM(booking_value) AS guest__booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - guest_id - ) subq_19 - ON - subq_13.guest = subq_19.guest -) subq_20 -WHERE guest__booking_value > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.guest_id = cm_3_cte.guest + ) subq_20 + WHERE guest__booking_value > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_filter_with_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_filter_with_conversion_metric__plan0_optimized.sql index b022e93fa..bec116d97 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_filter_with_conversion_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_filter_with_conversion_metric__plan0_optimized.sql @@ -2,26 +2,13 @@ test_name: test_filter_with_conversion_metric test_filename: test_metric_filter_rendering.py sql_engine: Databricks --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Compute Metrics via Expressions SELECT - CAST(subq_39.buys AS DOUBLE) / CAST(NULLIF(subq_39.visits, 0) AS DOUBLE) AS user__visit_buy_conversion_rate - , subq_24.listings AS listings + subq_39.user + , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS user__visit_buy_conversion_rate FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_24 - LEFT OUTER JOIN ( -- Combine Aggregated Outputs SELECT COALESCE(subq_28.user, subq_38.user) AS user @@ -116,7 +103,36 @@ FROM ( GROUP BY COALESCE(subq_28.user, subq_38.user) ) subq_39 - ON - subq_24.user = subq_39.user -) subq_42 -WHERE user__visit_buy_conversion_rate > 2 +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.user__visit_buy_conversion_rate AS user__visit_buy_conversion_rate + , subq_24.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_24 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_24.user = cm_3_cte.user + ) subq_42 + WHERE user__visit_buy_conversion_rate > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_group_by_has_local_entity_prefix__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_group_by_has_local_entity_prefix__plan0_optimized.sql index 6ae5114cd..2f450ddd8 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_group_by_has_local_entity_prefix__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_group_by_has_local_entity_prefix__plan0_optimized.sql @@ -2,43 +2,52 @@ test_name: test_group_by_has_local_entity_prefix test_filename: test_metric_filter_rendering.py sql_engine: Databricks --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['average_booking_value', 'listing__user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listings_latest_src_28000.user_id AS listing__user + , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id + GROUP BY + listings_latest_src_28000.user_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_27.listing__user__average_booking_value AS user__listing__user__average_booking_value - , subq_17.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_17 - LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['average_booking_value', 'listing__user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing__user', 'listing__user__average_booking_value'] SELECT - listings_latest_src_28000.user_id AS listing__user - , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + cm_3_cte.listing__user__average_booking_value AS user__listing__user__average_booking_value + , subq_17.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_17 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + cm_3_cte cm_3_cte ON - bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id - GROUP BY - listings_latest_src_28000.user_id - ) subq_27 - ON - subq_17.user = subq_27.listing__user -) subq_28 -WHERE user__listing__user__average_booking_value > 1 + subq_17.user = cm_3_cte.listing__user + ) subq_28 + WHERE user__listing__user__average_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_inner_query_multi_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_inner_query_multi_hop__plan0_optimized.sql index ae0625a96..e2f526bda 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_inner_query_multi_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_inner_query_multi_hop__plan0_optimized.sql @@ -4,51 +4,60 @@ docstring: Tests rendering for a metric filter using a two-hop join in the inner query. sql_engine: Databricks --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_35.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 + subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id + , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count + FROM ***************************.account_month_txns account_month_txns_src_22000 LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['account_id__customer_id__customer_third_hop_id', 'account_id__customer_id__customer_third_hop_id__txn_count'] + -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] SELECT - subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id - , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count - FROM ***************************.account_month_txns account_month_txns_src_22000 - LEFT OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] - SELECT - DATE_TRUNC('day', bridge_table_src_22000.ds_partitioned) AS ds_partitioned__day - , bridge_table_src_22000.account_id AS account_id - , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id - FROM ***************************.bridge_table bridge_table_src_22000 - LEFT OUTER JOIN - ***************************.customer_other_data customer_other_data_src_22000 - ON - bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id - ) subq_30 + DATE_TRUNC('day', bridge_table_src_22000.ds_partitioned) AS ds_partitioned__day + , bridge_table_src_22000.account_id AS account_id + , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id + FROM ***************************.bridge_table bridge_table_src_22000 + LEFT OUTER JOIN + ***************************.customer_other_data customer_other_data_src_22000 ON - ( - account_month_txns_src_22000.account_id = subq_30.account_id - ) AND ( - DATE_TRUNC('day', account_month_txns_src_22000.ds_partitioned) = subq_30.ds_partitioned__day - ) - GROUP BY - subq_30.customer_id__customer_third_hop_id - ) subq_35 + bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id + ) subq_30 ON - third_hop_table_src_22000.customer_third_hop_id = subq_35.account_id__customer_id__customer_third_hop_id -) subq_36 -WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 + ( + account_month_txns_src_22000.account_id = subq_30.account_id + ) AND ( + DATE_TRUNC('day', account_month_txns_src_22000.ds_partitioned) = subq_30.ds_partitioned__day + ) + GROUP BY + subq_30.customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.account_id__customer_id__customer_third_hop_id + ) subq_36 + WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_inner_query_single_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_inner_query_single_hop__plan0_optimized.sql index 32d0d4fd4..10a88382d 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_inner_query_single_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_inner_query_single_hop__plan0_optimized.sql @@ -4,41 +4,50 @@ docstring: Tests rendering for a metric filter using a one-hop join in the inner query. sql_engine: Databricks --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + customer_id__customer_third_hop_id + , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers + FROM ( + -- Read Elements From Semantic Model 'customer_other_data' + -- Metric Time Dimension 'acquired_ds' + SELECT + customer_third_hop_id AS customer_id__customer_third_hop_id + , country AS customer_id__country + , 1 AS customers_with_other_data + FROM ***************************.customer_other_data customer_other_data_src_22000 + ) subq_16 + WHERE customer_id__country = 'paraguay' + GROUP BY + customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['customer_id__customer_third_hop_id', 'customer_id__customer_third_hop_id__paraguayan_customers'] + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs SELECT - customer_id__customer_third_hop_id - , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers - FROM ( - -- Read Elements From Semantic Model 'customer_other_data' - -- Metric Time Dimension 'acquired_ds' - SELECT - customer_third_hop_id AS customer_id__customer_third_hop_id - , country AS customer_id__country - , 1 AS customers_with_other_data - FROM ***************************.customer_other_data customer_other_data_src_22000 - ) subq_16 - WHERE customer_id__country = 'paraguay' - GROUP BY - customer_id__customer_third_hop_id - ) subq_21 - ON - third_hop_table_src_22000.customer_third_hop_id = subq_21.customer_id__customer_third_hop_id -) subq_22 -WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 + cm_3_cte.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.customer_id__customer_third_hop_id + ) subq_22 + WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_metric_filtered_by_itself__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_metric_filtered_by_itself__plan0_optimized.sql index 8dd5d7b83..ee6d32d2c 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_metric_filtered_by_itself__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_metric_filtered_by_itself__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query for a metric that filters by the same metric. sql_engine: Databricks --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookers AS listing__bookers - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - listing_id AS listing - , guest_id AS bookers + cm_3_cte.listing__bookers AS listing__bookers + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookers > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.listing_id = cm_3_cte.listing + ) subq_20 + WHERE listing__bookers > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_metric_with_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_metric_with_metric_in_where_filter__plan0_optimized.sql index 27e287e27..08db85ffc 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_metric_with_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_metric_with_metric_in_where_filter__plan0_optimized.sql @@ -4,50 +4,60 @@ docstring: Tests a query with a metric in the metric-level where filter. sql_engine: Databricks --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings', 'metric_time__day'] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - metric_time__day - , SUM(listings) AS active_listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.metric_time__day AS metric_time__day - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT - DATE_TRUNC('day', created_at) AS metric_time__day - , listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(listings) AS active_listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.metric_time__day AS metric_time__day + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT - listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 -GROUP BY - metric_time__day + DATE_TRUNC('day', created_at) AS metric_time__day + , listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 + GROUP BY + metric_time__day +) + +SELECT + metric_time__day AS metric_time__day + , active_listings AS active_listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql index e792136fa..2ea48964e 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql @@ -6,40 +6,49 @@ docstring: Note this cumulative metric has no window / grain to date. sql_engine: Databricks --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'revenue' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['txn_revenue', 'user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + user_id AS user + , SUM(revenue) AS user__revenue_all_time + FROM ***************************.fct_revenue revenue_src_28000 + GROUP BY + user_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.user__revenue_all_time AS user__revenue_all_time - , subq_13.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'revenue' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['txn_revenue', 'user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['user', 'user__revenue_all_time'] - SELECT - user_id AS user - , SUM(revenue) AS user__revenue_all_time - FROM ***************************.fct_revenue revenue_src_28000 - GROUP BY - user_id - ) subq_19 - ON - subq_13.user = subq_19.user -) subq_20 -WHERE user__revenue_all_time > 1 + cm_3_cte.user__revenue_all_time AS user__revenue_all_time + , subq_13.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.user = cm_3_cte.user + ) subq_20 + WHERE user__revenue_all_time > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql index 924ebfbad..66141875f 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql @@ -4,75 +4,89 @@ docstring: Tests a query with a derived metric in the query-level where filter. sql_engine: Databricks --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_33.listing__views_times_booking_value AS listing__views_times_booking_value - , subq_20.listings AS listings + listing + , SUM(views) AS views FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'views_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['views', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__views_times_booking_value'] + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_28 + GROUP BY + listing +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing + , booking_value * views AS listing__views_times_booking_value + FROM ( + -- Combine Aggregated Outputs SELECT - listing - , booking_value * views AS listing__views_times_booking_value + COALESCE(cm_5_cte.listing, cm_6_cte.listing) AS listing + , MAX(cm_5_cte.booking_value) AS booking_value + , MAX(cm_6_cte.views) AS views + FROM cm_5_cte cm_5_cte + FULL OUTER JOIN + cm_6_cte cm_6_cte + ON + cm_5_cte.listing = cm_6_cte.listing + GROUP BY + COALESCE(cm_5_cte.listing, cm_6_cte.listing) + ) subq_31 +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_7_cte.listing__views_times_booking_value AS listing__views_times_booking_value + , subq_20.listings AS listings FROM ( - -- Combine Aggregated Outputs + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' SELECT - COALESCE(subq_25.listing, subq_30.listing) AS listing - , MAX(subq_25.booking_value) AS booking_value - , MAX(subq_30.views) AS views - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing_id AS listing - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_25 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing - , SUM(views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['views', 'listing'] - SELECT - listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_28 - GROUP BY - listing - ) subq_30 - ON - subq_25.listing = subq_30.listing - GROUP BY - COALESCE(subq_25.listing, subq_30.listing) - ) subq_31 - ) subq_33 - ON - subq_20.listing = subq_33.listing -) subq_34 -WHERE listing__views_times_booking_value > 1 + listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_7_cte cm_7_cte + ON + subq_20.listing = cm_7_cte.listing + ) subq_34 + WHERE listing__views_times_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql index 355684fa4..f82138ee3 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql @@ -4,62 +4,73 @@ docstring: Tests a query with 2 simple metrics in the query-level where filter. sql_engine: Databricks --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_6 +WITH cm_4_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_25.listing__bookings AS listing__bookings - , subq_31.listing__bookers AS listing__bookers - , subq_19.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_19 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_22 + GROUP BY + listing +) + +, cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_4_cte.listing__bookings AS listing__bookings + , cm_5_cte.listing__bookers AS listing__bookers + , subq_19.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_22 - GROUP BY - listing - ) subq_25 - ON - subq_19.listing = subq_25.listing - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_31 - ON - subq_19.listing = subq_31.listing -) subq_32 -WHERE listing__bookings > 2 AND listing__bookers > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_19 + LEFT OUTER JOIN + cm_4_cte cm_4_cte + ON + subq_19.listing = cm_4_cte.listing + LEFT OUTER JOIN + cm_5_cte cm_5_cte + ON + subq_19.listing = cm_5_cte.listing + ) subq_32 + WHERE listing__bookings > 2 AND listing__bookers > 1 +) + +SELECT + listings AS listings +FROM cm_6_cte cm_6_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql index 7007d806d..35ef8d482 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql @@ -4,46 +4,65 @@ docstring: Tests a query with a ratio metric in the query-level where filter. sql_engine: Databricks --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_9 +WITH cm_7_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - CAST(subq_25.bookings AS DOUBLE) / CAST(NULLIF(subq_25.bookers, 0) AS DOUBLE) AS listing__bookings_per_booker - , subq_20.listings AS listings + listing + , SUM(bookings) AS bookings + , COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + , 1 AS bookings + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_23 + GROUP BY + listing +) + +, cm_8_cte AS ( + -- Read From CTE For node_id=cm_7 + -- Compute Metrics via Expressions + SELECT + listing + , CAST(bookings AS DOUBLE) / CAST(NULLIF(bookers, 0) AS DOUBLE) AS listing__bookings_per_booker + FROM cm_7_cte cm_7_cte +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS bookings - , COUNT(DISTINCT bookers) AS bookers + cm_8_cte.listing__bookings_per_booker AS listing__bookings_per_booker + , subq_20.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - GROUP BY - listing - ) subq_25 - ON - subq_20.listing = subq_25.listing -) subq_28 -WHERE listing__bookings_per_booker > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_8_cte cm_8_cte + ON + subq_20.listing = cm_8_cte.listing + ) subq_28 + WHERE listing__bookings_per_booker > 1 +) + +SELECT + listings AS listings +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql index 2db347fc9..61e2f4e78 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql @@ -4,45 +4,54 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: Databricks --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql index f97c47970..8b36291e9 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: Postgres --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'guest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + guest_id AS guest + , SUM(booking_value) AS guest__booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + guest_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.guest__booking_value AS guest__booking_value - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - guest_id AS guest - , guest_id AS bookers + cm_3_cte.guest__booking_value AS guest__booking_value + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'guest'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['guest', 'guest__booking_value'] - SELECT - guest_id AS guest - , SUM(booking_value) AS guest__booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - guest_id - ) subq_19 - ON - subq_13.guest = subq_19.guest -) subq_20 -WHERE guest__booking_value > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.guest_id = cm_3_cte.guest + ) subq_20 + WHERE guest__booking_value > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_filter_with_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_filter_with_conversion_metric__plan0_optimized.sql index f6ef6f9ef..ffda9d300 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_filter_with_conversion_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_filter_with_conversion_metric__plan0_optimized.sql @@ -2,26 +2,13 @@ test_name: test_filter_with_conversion_metric test_filename: test_metric_filter_rendering.py sql_engine: Postgres --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Compute Metrics via Expressions SELECT - CAST(subq_39.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_39.visits, 0) AS DOUBLE PRECISION) AS user__visit_buy_conversion_rate - , subq_24.listings AS listings + subq_39.user + , CAST(buys AS DOUBLE PRECISION) / CAST(NULLIF(visits, 0) AS DOUBLE PRECISION) AS user__visit_buy_conversion_rate FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_24 - LEFT OUTER JOIN ( -- Combine Aggregated Outputs SELECT COALESCE(subq_28.user, subq_38.user) AS user @@ -116,7 +103,36 @@ FROM ( GROUP BY COALESCE(subq_28.user, subq_38.user) ) subq_39 - ON - subq_24.user = subq_39.user -) subq_42 -WHERE user__visit_buy_conversion_rate > 2 +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.user__visit_buy_conversion_rate AS user__visit_buy_conversion_rate + , subq_24.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_24 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_24.user = cm_3_cte.user + ) subq_42 + WHERE user__visit_buy_conversion_rate > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_group_by_has_local_entity_prefix__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_group_by_has_local_entity_prefix__plan0_optimized.sql index 557149d12..0bbe2c967 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_group_by_has_local_entity_prefix__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_group_by_has_local_entity_prefix__plan0_optimized.sql @@ -2,43 +2,52 @@ test_name: test_group_by_has_local_entity_prefix test_filename: test_metric_filter_rendering.py sql_engine: Postgres --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['average_booking_value', 'listing__user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listings_latest_src_28000.user_id AS listing__user + , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id + GROUP BY + listings_latest_src_28000.user_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_27.listing__user__average_booking_value AS user__listing__user__average_booking_value - , subq_17.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_17 - LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['average_booking_value', 'listing__user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing__user', 'listing__user__average_booking_value'] SELECT - listings_latest_src_28000.user_id AS listing__user - , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + cm_3_cte.listing__user__average_booking_value AS user__listing__user__average_booking_value + , subq_17.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_17 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + cm_3_cte cm_3_cte ON - bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id - GROUP BY - listings_latest_src_28000.user_id - ) subq_27 - ON - subq_17.user = subq_27.listing__user -) subq_28 -WHERE user__listing__user__average_booking_value > 1 + subq_17.user = cm_3_cte.listing__user + ) subq_28 + WHERE user__listing__user__average_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_inner_query_multi_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_inner_query_multi_hop__plan0_optimized.sql index 5ab04bcc9..a64abb1d6 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_inner_query_multi_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_inner_query_multi_hop__plan0_optimized.sql @@ -4,51 +4,60 @@ docstring: Tests rendering for a metric filter using a two-hop join in the inner query. sql_engine: Postgres --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_35.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 + subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id + , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count + FROM ***************************.account_month_txns account_month_txns_src_22000 LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['account_id__customer_id__customer_third_hop_id', 'account_id__customer_id__customer_third_hop_id__txn_count'] + -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] SELECT - subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id - , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count - FROM ***************************.account_month_txns account_month_txns_src_22000 - LEFT OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] - SELECT - DATE_TRUNC('day', bridge_table_src_22000.ds_partitioned) AS ds_partitioned__day - , bridge_table_src_22000.account_id AS account_id - , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id - FROM ***************************.bridge_table bridge_table_src_22000 - LEFT OUTER JOIN - ***************************.customer_other_data customer_other_data_src_22000 - ON - bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id - ) subq_30 + DATE_TRUNC('day', bridge_table_src_22000.ds_partitioned) AS ds_partitioned__day + , bridge_table_src_22000.account_id AS account_id + , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id + FROM ***************************.bridge_table bridge_table_src_22000 + LEFT OUTER JOIN + ***************************.customer_other_data customer_other_data_src_22000 ON - ( - account_month_txns_src_22000.account_id = subq_30.account_id - ) AND ( - DATE_TRUNC('day', account_month_txns_src_22000.ds_partitioned) = subq_30.ds_partitioned__day - ) - GROUP BY - subq_30.customer_id__customer_third_hop_id - ) subq_35 + bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id + ) subq_30 ON - third_hop_table_src_22000.customer_third_hop_id = subq_35.account_id__customer_id__customer_third_hop_id -) subq_36 -WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 + ( + account_month_txns_src_22000.account_id = subq_30.account_id + ) AND ( + DATE_TRUNC('day', account_month_txns_src_22000.ds_partitioned) = subq_30.ds_partitioned__day + ) + GROUP BY + subq_30.customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.account_id__customer_id__customer_third_hop_id + ) subq_36 + WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_inner_query_single_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_inner_query_single_hop__plan0_optimized.sql index 8fb6eae17..f5083603d 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_inner_query_single_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_inner_query_single_hop__plan0_optimized.sql @@ -4,41 +4,50 @@ docstring: Tests rendering for a metric filter using a one-hop join in the inner query. sql_engine: Postgres --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + customer_id__customer_third_hop_id + , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers + FROM ( + -- Read Elements From Semantic Model 'customer_other_data' + -- Metric Time Dimension 'acquired_ds' + SELECT + customer_third_hop_id AS customer_id__customer_third_hop_id + , country AS customer_id__country + , 1 AS customers_with_other_data + FROM ***************************.customer_other_data customer_other_data_src_22000 + ) subq_16 + WHERE customer_id__country = 'paraguay' + GROUP BY + customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['customer_id__customer_third_hop_id', 'customer_id__customer_third_hop_id__paraguayan_customers'] + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs SELECT - customer_id__customer_third_hop_id - , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers - FROM ( - -- Read Elements From Semantic Model 'customer_other_data' - -- Metric Time Dimension 'acquired_ds' - SELECT - customer_third_hop_id AS customer_id__customer_third_hop_id - , country AS customer_id__country - , 1 AS customers_with_other_data - FROM ***************************.customer_other_data customer_other_data_src_22000 - ) subq_16 - WHERE customer_id__country = 'paraguay' - GROUP BY - customer_id__customer_third_hop_id - ) subq_21 - ON - third_hop_table_src_22000.customer_third_hop_id = subq_21.customer_id__customer_third_hop_id -) subq_22 -WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 + cm_3_cte.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.customer_id__customer_third_hop_id + ) subq_22 + WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_metric_filtered_by_itself__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_metric_filtered_by_itself__plan0_optimized.sql index 7e92b1649..4f6cba2d8 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_metric_filtered_by_itself__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_metric_filtered_by_itself__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query for a metric that filters by the same metric. sql_engine: Postgres --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookers AS listing__bookers - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - listing_id AS listing - , guest_id AS bookers + cm_3_cte.listing__bookers AS listing__bookers + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookers > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.listing_id = cm_3_cte.listing + ) subq_20 + WHERE listing__bookers > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_metric_with_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_metric_with_metric_in_where_filter__plan0_optimized.sql index 74bc392a5..94ea4b491 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_metric_with_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_metric_with_metric_in_where_filter__plan0_optimized.sql @@ -4,50 +4,60 @@ docstring: Tests a query with a metric in the metric-level where filter. sql_engine: Postgres --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings', 'metric_time__day'] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - metric_time__day - , SUM(listings) AS active_listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.metric_time__day AS metric_time__day - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT - DATE_TRUNC('day', created_at) AS metric_time__day - , listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(listings) AS active_listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.metric_time__day AS metric_time__day + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT - listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 -GROUP BY - metric_time__day + DATE_TRUNC('day', created_at) AS metric_time__day + , listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 + GROUP BY + metric_time__day +) + +SELECT + metric_time__day AS metric_time__day + , active_listings AS active_listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql index 9b3ce5aaa..436298fc4 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql @@ -6,40 +6,49 @@ docstring: Note this cumulative metric has no window / grain to date. sql_engine: Postgres --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'revenue' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['txn_revenue', 'user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + user_id AS user + , SUM(revenue) AS user__revenue_all_time + FROM ***************************.fct_revenue revenue_src_28000 + GROUP BY + user_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.user__revenue_all_time AS user__revenue_all_time - , subq_13.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'revenue' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['txn_revenue', 'user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['user', 'user__revenue_all_time'] - SELECT - user_id AS user - , SUM(revenue) AS user__revenue_all_time - FROM ***************************.fct_revenue revenue_src_28000 - GROUP BY - user_id - ) subq_19 - ON - subq_13.user = subq_19.user -) subq_20 -WHERE user__revenue_all_time > 1 + cm_3_cte.user__revenue_all_time AS user__revenue_all_time + , subq_13.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.user = cm_3_cte.user + ) subq_20 + WHERE user__revenue_all_time > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql index b0a0b216f..0befd979b 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql @@ -4,75 +4,89 @@ docstring: Tests a query with a derived metric in the query-level where filter. sql_engine: Postgres --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_33.listing__views_times_booking_value AS listing__views_times_booking_value - , subq_20.listings AS listings + listing + , SUM(views) AS views FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'views_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['views', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__views_times_booking_value'] + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_28 + GROUP BY + listing +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing + , booking_value * views AS listing__views_times_booking_value + FROM ( + -- Combine Aggregated Outputs SELECT - listing - , booking_value * views AS listing__views_times_booking_value + COALESCE(cm_5_cte.listing, cm_6_cte.listing) AS listing + , MAX(cm_5_cte.booking_value) AS booking_value + , MAX(cm_6_cte.views) AS views + FROM cm_5_cte cm_5_cte + FULL OUTER JOIN + cm_6_cte cm_6_cte + ON + cm_5_cte.listing = cm_6_cte.listing + GROUP BY + COALESCE(cm_5_cte.listing, cm_6_cte.listing) + ) subq_31 +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_7_cte.listing__views_times_booking_value AS listing__views_times_booking_value + , subq_20.listings AS listings FROM ( - -- Combine Aggregated Outputs + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' SELECT - COALESCE(subq_25.listing, subq_30.listing) AS listing - , MAX(subq_25.booking_value) AS booking_value - , MAX(subq_30.views) AS views - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing_id AS listing - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_25 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing - , SUM(views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['views', 'listing'] - SELECT - listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_28 - GROUP BY - listing - ) subq_30 - ON - subq_25.listing = subq_30.listing - GROUP BY - COALESCE(subq_25.listing, subq_30.listing) - ) subq_31 - ) subq_33 - ON - subq_20.listing = subq_33.listing -) subq_34 -WHERE listing__views_times_booking_value > 1 + listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_7_cte cm_7_cte + ON + subq_20.listing = cm_7_cte.listing + ) subq_34 + WHERE listing__views_times_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql index cdb2978e0..8a0c51dc5 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql @@ -4,62 +4,73 @@ docstring: Tests a query with 2 simple metrics in the query-level where filter. sql_engine: Postgres --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_6 +WITH cm_4_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_25.listing__bookings AS listing__bookings - , subq_31.listing__bookers AS listing__bookers - , subq_19.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_19 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_22 + GROUP BY + listing +) + +, cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_4_cte.listing__bookings AS listing__bookings + , cm_5_cte.listing__bookers AS listing__bookers + , subq_19.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_22 - GROUP BY - listing - ) subq_25 - ON - subq_19.listing = subq_25.listing - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_31 - ON - subq_19.listing = subq_31.listing -) subq_32 -WHERE listing__bookings > 2 AND listing__bookers > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_19 + LEFT OUTER JOIN + cm_4_cte cm_4_cte + ON + subq_19.listing = cm_4_cte.listing + LEFT OUTER JOIN + cm_5_cte cm_5_cte + ON + subq_19.listing = cm_5_cte.listing + ) subq_32 + WHERE listing__bookings > 2 AND listing__bookers > 1 +) + +SELECT + listings AS listings +FROM cm_6_cte cm_6_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql index 038d123f1..b4fea3dda 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql @@ -4,46 +4,65 @@ docstring: Tests a query with a ratio metric in the query-level where filter. sql_engine: Postgres --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_9 +WITH cm_7_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - CAST(subq_25.bookings AS DOUBLE PRECISION) / CAST(NULLIF(subq_25.bookers, 0) AS DOUBLE PRECISION) AS listing__bookings_per_booker - , subq_20.listings AS listings + listing + , SUM(bookings) AS bookings + , COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + , 1 AS bookings + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_23 + GROUP BY + listing +) + +, cm_8_cte AS ( + -- Read From CTE For node_id=cm_7 + -- Compute Metrics via Expressions + SELECT + listing + , CAST(bookings AS DOUBLE PRECISION) / CAST(NULLIF(bookers, 0) AS DOUBLE PRECISION) AS listing__bookings_per_booker + FROM cm_7_cte cm_7_cte +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS bookings - , COUNT(DISTINCT bookers) AS bookers + cm_8_cte.listing__bookings_per_booker AS listing__bookings_per_booker + , subq_20.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - GROUP BY - listing - ) subq_25 - ON - subq_20.listing = subq_25.listing -) subq_28 -WHERE listing__bookings_per_booker > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_8_cte cm_8_cte + ON + subq_20.listing = cm_8_cte.listing + ) subq_28 + WHERE listing__bookings_per_booker > 1 +) + +SELECT + listings AS listings +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql index 38f0a21af..1bcdd6765 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql @@ -4,45 +4,54 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: Postgres --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql index 25bcd2eb7..b1fbf8f8a 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: Redshift --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'guest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + guest_id AS guest + , SUM(booking_value) AS guest__booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + guest_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.guest__booking_value AS guest__booking_value - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - guest_id AS guest - , guest_id AS bookers + cm_3_cte.guest__booking_value AS guest__booking_value + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'guest'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['guest', 'guest__booking_value'] - SELECT - guest_id AS guest - , SUM(booking_value) AS guest__booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - guest_id - ) subq_19 - ON - subq_13.guest = subq_19.guest -) subq_20 -WHERE guest__booking_value > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.guest_id = cm_3_cte.guest + ) subq_20 + WHERE guest__booking_value > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_filter_with_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_filter_with_conversion_metric__plan0_optimized.sql index 1c4dc8b06..922d7fdb0 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_filter_with_conversion_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_filter_with_conversion_metric__plan0_optimized.sql @@ -2,26 +2,13 @@ test_name: test_filter_with_conversion_metric test_filename: test_metric_filter_rendering.py sql_engine: Redshift --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Compute Metrics via Expressions SELECT - CAST(subq_39.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_39.visits, 0) AS DOUBLE PRECISION) AS user__visit_buy_conversion_rate - , subq_24.listings AS listings + subq_39.user + , CAST(buys AS DOUBLE PRECISION) / CAST(NULLIF(visits, 0) AS DOUBLE PRECISION) AS user__visit_buy_conversion_rate FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_24 - LEFT OUTER JOIN ( -- Combine Aggregated Outputs SELECT COALESCE(subq_28.user, subq_38.user) AS user @@ -116,7 +103,36 @@ FROM ( GROUP BY COALESCE(subq_28.user, subq_38.user) ) subq_39 - ON - subq_24.user = subq_39.user -) subq_42 -WHERE user__visit_buy_conversion_rate > 2 +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.user__visit_buy_conversion_rate AS user__visit_buy_conversion_rate + , subq_24.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_24 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_24.user = cm_3_cte.user + ) subq_42 + WHERE user__visit_buy_conversion_rate > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_group_by_has_local_entity_prefix__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_group_by_has_local_entity_prefix__plan0_optimized.sql index f4f1f7429..91c697b80 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_group_by_has_local_entity_prefix__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_group_by_has_local_entity_prefix__plan0_optimized.sql @@ -2,43 +2,52 @@ test_name: test_group_by_has_local_entity_prefix test_filename: test_metric_filter_rendering.py sql_engine: Redshift --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['average_booking_value', 'listing__user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listings_latest_src_28000.user_id AS listing__user + , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id + GROUP BY + listings_latest_src_28000.user_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_27.listing__user__average_booking_value AS user__listing__user__average_booking_value - , subq_17.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_17 - LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['average_booking_value', 'listing__user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing__user', 'listing__user__average_booking_value'] SELECT - listings_latest_src_28000.user_id AS listing__user - , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + cm_3_cte.listing__user__average_booking_value AS user__listing__user__average_booking_value + , subq_17.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_17 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + cm_3_cte cm_3_cte ON - bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id - GROUP BY - listings_latest_src_28000.user_id - ) subq_27 - ON - subq_17.user = subq_27.listing__user -) subq_28 -WHERE user__listing__user__average_booking_value > 1 + subq_17.user = cm_3_cte.listing__user + ) subq_28 + WHERE user__listing__user__average_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_inner_query_multi_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_inner_query_multi_hop__plan0_optimized.sql index c89509a59..251bc7637 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_inner_query_multi_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_inner_query_multi_hop__plan0_optimized.sql @@ -4,51 +4,60 @@ docstring: Tests rendering for a metric filter using a two-hop join in the inner query. sql_engine: Redshift --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_35.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 + subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id + , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count + FROM ***************************.account_month_txns account_month_txns_src_22000 LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['account_id__customer_id__customer_third_hop_id', 'account_id__customer_id__customer_third_hop_id__txn_count'] + -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] SELECT - subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id - , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count - FROM ***************************.account_month_txns account_month_txns_src_22000 - LEFT OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] - SELECT - DATE_TRUNC('day', bridge_table_src_22000.ds_partitioned) AS ds_partitioned__day - , bridge_table_src_22000.account_id AS account_id - , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id - FROM ***************************.bridge_table bridge_table_src_22000 - LEFT OUTER JOIN - ***************************.customer_other_data customer_other_data_src_22000 - ON - bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id - ) subq_30 + DATE_TRUNC('day', bridge_table_src_22000.ds_partitioned) AS ds_partitioned__day + , bridge_table_src_22000.account_id AS account_id + , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id + FROM ***************************.bridge_table bridge_table_src_22000 + LEFT OUTER JOIN + ***************************.customer_other_data customer_other_data_src_22000 ON - ( - account_month_txns_src_22000.account_id = subq_30.account_id - ) AND ( - DATE_TRUNC('day', account_month_txns_src_22000.ds_partitioned) = subq_30.ds_partitioned__day - ) - GROUP BY - subq_30.customer_id__customer_third_hop_id - ) subq_35 + bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id + ) subq_30 ON - third_hop_table_src_22000.customer_third_hop_id = subq_35.account_id__customer_id__customer_third_hop_id -) subq_36 -WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 + ( + account_month_txns_src_22000.account_id = subq_30.account_id + ) AND ( + DATE_TRUNC('day', account_month_txns_src_22000.ds_partitioned) = subq_30.ds_partitioned__day + ) + GROUP BY + subq_30.customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.account_id__customer_id__customer_third_hop_id + ) subq_36 + WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_inner_query_single_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_inner_query_single_hop__plan0_optimized.sql index f0195e62a..ab3e7cffe 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_inner_query_single_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_inner_query_single_hop__plan0_optimized.sql @@ -4,41 +4,50 @@ docstring: Tests rendering for a metric filter using a one-hop join in the inner query. sql_engine: Redshift --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + customer_id__customer_third_hop_id + , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers + FROM ( + -- Read Elements From Semantic Model 'customer_other_data' + -- Metric Time Dimension 'acquired_ds' + SELECT + customer_third_hop_id AS customer_id__customer_third_hop_id + , country AS customer_id__country + , 1 AS customers_with_other_data + FROM ***************************.customer_other_data customer_other_data_src_22000 + ) subq_16 + WHERE customer_id__country = 'paraguay' + GROUP BY + customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['customer_id__customer_third_hop_id', 'customer_id__customer_third_hop_id__paraguayan_customers'] + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs SELECT - customer_id__customer_third_hop_id - , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers - FROM ( - -- Read Elements From Semantic Model 'customer_other_data' - -- Metric Time Dimension 'acquired_ds' - SELECT - customer_third_hop_id AS customer_id__customer_third_hop_id - , country AS customer_id__country - , 1 AS customers_with_other_data - FROM ***************************.customer_other_data customer_other_data_src_22000 - ) subq_16 - WHERE customer_id__country = 'paraguay' - GROUP BY - customer_id__customer_third_hop_id - ) subq_21 - ON - third_hop_table_src_22000.customer_third_hop_id = subq_21.customer_id__customer_third_hop_id -) subq_22 -WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 + cm_3_cte.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.customer_id__customer_third_hop_id + ) subq_22 + WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_metric_filtered_by_itself__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_metric_filtered_by_itself__plan0_optimized.sql index 3c0a42954..791e5f10c 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_metric_filtered_by_itself__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_metric_filtered_by_itself__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query for a metric that filters by the same metric. sql_engine: Redshift --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookers AS listing__bookers - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - listing_id AS listing - , guest_id AS bookers + cm_3_cte.listing__bookers AS listing__bookers + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookers > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.listing_id = cm_3_cte.listing + ) subq_20 + WHERE listing__bookers > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_metric_with_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_metric_with_metric_in_where_filter__plan0_optimized.sql index 40a62e346..5c207a35e 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_metric_with_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_metric_with_metric_in_where_filter__plan0_optimized.sql @@ -4,50 +4,60 @@ docstring: Tests a query with a metric in the metric-level where filter. sql_engine: Redshift --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings', 'metric_time__day'] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - metric_time__day - , SUM(listings) AS active_listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.metric_time__day AS metric_time__day - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT - DATE_TRUNC('day', created_at) AS metric_time__day - , listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(listings) AS active_listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.metric_time__day AS metric_time__day + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT - listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 -GROUP BY - metric_time__day + DATE_TRUNC('day', created_at) AS metric_time__day + , listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 + GROUP BY + metric_time__day +) + +SELECT + metric_time__day AS metric_time__day + , active_listings AS active_listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql index ed899a05b..b63cdb18d 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql @@ -6,40 +6,49 @@ docstring: Note this cumulative metric has no window / grain to date. sql_engine: Redshift --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'revenue' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['txn_revenue', 'user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + user_id AS user + , SUM(revenue) AS user__revenue_all_time + FROM ***************************.fct_revenue revenue_src_28000 + GROUP BY + user_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.user__revenue_all_time AS user__revenue_all_time - , subq_13.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'revenue' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['txn_revenue', 'user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['user', 'user__revenue_all_time'] - SELECT - user_id AS user - , SUM(revenue) AS user__revenue_all_time - FROM ***************************.fct_revenue revenue_src_28000 - GROUP BY - user_id - ) subq_19 - ON - subq_13.user = subq_19.user -) subq_20 -WHERE user__revenue_all_time > 1 + cm_3_cte.user__revenue_all_time AS user__revenue_all_time + , subq_13.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.user = cm_3_cte.user + ) subq_20 + WHERE user__revenue_all_time > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql index fe3105003..530903719 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql @@ -4,75 +4,89 @@ docstring: Tests a query with a derived metric in the query-level where filter. sql_engine: Redshift --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_33.listing__views_times_booking_value AS listing__views_times_booking_value - , subq_20.listings AS listings + listing + , SUM(views) AS views FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'views_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['views', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__views_times_booking_value'] + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_28 + GROUP BY + listing +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing + , booking_value * views AS listing__views_times_booking_value + FROM ( + -- Combine Aggregated Outputs SELECT - listing - , booking_value * views AS listing__views_times_booking_value + COALESCE(cm_5_cte.listing, cm_6_cte.listing) AS listing + , MAX(cm_5_cte.booking_value) AS booking_value + , MAX(cm_6_cte.views) AS views + FROM cm_5_cte cm_5_cte + FULL OUTER JOIN + cm_6_cte cm_6_cte + ON + cm_5_cte.listing = cm_6_cte.listing + GROUP BY + COALESCE(cm_5_cte.listing, cm_6_cte.listing) + ) subq_31 +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_7_cte.listing__views_times_booking_value AS listing__views_times_booking_value + , subq_20.listings AS listings FROM ( - -- Combine Aggregated Outputs + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' SELECT - COALESCE(subq_25.listing, subq_30.listing) AS listing - , MAX(subq_25.booking_value) AS booking_value - , MAX(subq_30.views) AS views - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing_id AS listing - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_25 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing - , SUM(views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['views', 'listing'] - SELECT - listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_28 - GROUP BY - listing - ) subq_30 - ON - subq_25.listing = subq_30.listing - GROUP BY - COALESCE(subq_25.listing, subq_30.listing) - ) subq_31 - ) subq_33 - ON - subq_20.listing = subq_33.listing -) subq_34 -WHERE listing__views_times_booking_value > 1 + listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_7_cte cm_7_cte + ON + subq_20.listing = cm_7_cte.listing + ) subq_34 + WHERE listing__views_times_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql index fe7d7cbb3..b421b7615 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql @@ -4,62 +4,73 @@ docstring: Tests a query with 2 simple metrics in the query-level where filter. sql_engine: Redshift --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_6 +WITH cm_4_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_25.listing__bookings AS listing__bookings - , subq_31.listing__bookers AS listing__bookers - , subq_19.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_19 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_22 + GROUP BY + listing +) + +, cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_4_cte.listing__bookings AS listing__bookings + , cm_5_cte.listing__bookers AS listing__bookers + , subq_19.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_22 - GROUP BY - listing - ) subq_25 - ON - subq_19.listing = subq_25.listing - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_31 - ON - subq_19.listing = subq_31.listing -) subq_32 -WHERE listing__bookings > 2 AND listing__bookers > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_19 + LEFT OUTER JOIN + cm_4_cte cm_4_cte + ON + subq_19.listing = cm_4_cte.listing + LEFT OUTER JOIN + cm_5_cte cm_5_cte + ON + subq_19.listing = cm_5_cte.listing + ) subq_32 + WHERE listing__bookings > 2 AND listing__bookers > 1 +) + +SELECT + listings AS listings +FROM cm_6_cte cm_6_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql index 5b859eca2..1be791e97 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql @@ -4,46 +4,65 @@ docstring: Tests a query with a ratio metric in the query-level where filter. sql_engine: Redshift --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_9 +WITH cm_7_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - CAST(subq_25.bookings AS DOUBLE PRECISION) / CAST(NULLIF(subq_25.bookers, 0) AS DOUBLE PRECISION) AS listing__bookings_per_booker - , subq_20.listings AS listings + listing + , SUM(bookings) AS bookings + , COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + , 1 AS bookings + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_23 + GROUP BY + listing +) + +, cm_8_cte AS ( + -- Read From CTE For node_id=cm_7 + -- Compute Metrics via Expressions + SELECT + listing + , CAST(bookings AS DOUBLE PRECISION) / CAST(NULLIF(bookers, 0) AS DOUBLE PRECISION) AS listing__bookings_per_booker + FROM cm_7_cte cm_7_cte +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS bookings - , COUNT(DISTINCT bookers) AS bookers + cm_8_cte.listing__bookings_per_booker AS listing__bookings_per_booker + , subq_20.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - GROUP BY - listing - ) subq_25 - ON - subq_20.listing = subq_25.listing -) subq_28 -WHERE listing__bookings_per_booker > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_8_cte cm_8_cte + ON + subq_20.listing = cm_8_cte.listing + ) subq_28 + WHERE listing__bookings_per_booker > 1 +) + +SELECT + listings AS listings +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql index a7832d3cb..66df6046d 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql @@ -4,45 +4,54 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: Redshift --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql index 4cf166658..e1b25e29b 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: Snowflake --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'guest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + guest_id AS guest + , SUM(booking_value) AS guest__booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + guest_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.guest__booking_value AS guest__booking_value - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - guest_id AS guest - , guest_id AS bookers + cm_3_cte.guest__booking_value AS guest__booking_value + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'guest'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['guest', 'guest__booking_value'] - SELECT - guest_id AS guest - , SUM(booking_value) AS guest__booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - guest_id - ) subq_19 - ON - subq_13.guest = subq_19.guest -) subq_20 -WHERE guest__booking_value > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.guest_id = cm_3_cte.guest + ) subq_20 + WHERE guest__booking_value > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_filter_with_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_filter_with_conversion_metric__plan0_optimized.sql index 149f48bbb..e8c3b0bff 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_filter_with_conversion_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_filter_with_conversion_metric__plan0_optimized.sql @@ -2,26 +2,13 @@ test_name: test_filter_with_conversion_metric test_filename: test_metric_filter_rendering.py sql_engine: Snowflake --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Compute Metrics via Expressions SELECT - CAST(subq_39.buys AS DOUBLE) / CAST(NULLIF(subq_39.visits, 0) AS DOUBLE) AS user__visit_buy_conversion_rate - , subq_24.listings AS listings + subq_39.user + , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS user__visit_buy_conversion_rate FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_24 - LEFT OUTER JOIN ( -- Combine Aggregated Outputs SELECT COALESCE(subq_28.user, subq_38.user) AS user @@ -116,7 +103,36 @@ FROM ( GROUP BY COALESCE(subq_28.user, subq_38.user) ) subq_39 - ON - subq_24.user = subq_39.user -) subq_42 -WHERE user__visit_buy_conversion_rate > 2 +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.user__visit_buy_conversion_rate AS user__visit_buy_conversion_rate + , subq_24.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_24 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_24.user = cm_3_cte.user + ) subq_42 + WHERE user__visit_buy_conversion_rate > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_group_by_has_local_entity_prefix__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_group_by_has_local_entity_prefix__plan0_optimized.sql index 7f9be66b9..516eb1221 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_group_by_has_local_entity_prefix__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_group_by_has_local_entity_prefix__plan0_optimized.sql @@ -2,43 +2,52 @@ test_name: test_group_by_has_local_entity_prefix test_filename: test_metric_filter_rendering.py sql_engine: Snowflake --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['average_booking_value', 'listing__user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listings_latest_src_28000.user_id AS listing__user + , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id + GROUP BY + listings_latest_src_28000.user_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_27.listing__user__average_booking_value AS user__listing__user__average_booking_value - , subq_17.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_17 - LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['average_booking_value', 'listing__user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing__user', 'listing__user__average_booking_value'] SELECT - listings_latest_src_28000.user_id AS listing__user - , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + cm_3_cte.listing__user__average_booking_value AS user__listing__user__average_booking_value + , subq_17.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_17 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + cm_3_cte cm_3_cte ON - bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id - GROUP BY - listings_latest_src_28000.user_id - ) subq_27 - ON - subq_17.user = subq_27.listing__user -) subq_28 -WHERE user__listing__user__average_booking_value > 1 + subq_17.user = cm_3_cte.listing__user + ) subq_28 + WHERE user__listing__user__average_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_inner_query_multi_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_inner_query_multi_hop__plan0_optimized.sql index b2b219ffc..6e9f5bb07 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_inner_query_multi_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_inner_query_multi_hop__plan0_optimized.sql @@ -4,51 +4,60 @@ docstring: Tests rendering for a metric filter using a two-hop join in the inner query. sql_engine: Snowflake --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_35.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 + subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id + , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count + FROM ***************************.account_month_txns account_month_txns_src_22000 LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['account_id__customer_id__customer_third_hop_id', 'account_id__customer_id__customer_third_hop_id__txn_count'] + -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] SELECT - subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id - , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count - FROM ***************************.account_month_txns account_month_txns_src_22000 - LEFT OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] - SELECT - DATE_TRUNC('day', bridge_table_src_22000.ds_partitioned) AS ds_partitioned__day - , bridge_table_src_22000.account_id AS account_id - , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id - FROM ***************************.bridge_table bridge_table_src_22000 - LEFT OUTER JOIN - ***************************.customer_other_data customer_other_data_src_22000 - ON - bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id - ) subq_30 + DATE_TRUNC('day', bridge_table_src_22000.ds_partitioned) AS ds_partitioned__day + , bridge_table_src_22000.account_id AS account_id + , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id + FROM ***************************.bridge_table bridge_table_src_22000 + LEFT OUTER JOIN + ***************************.customer_other_data customer_other_data_src_22000 ON - ( - account_month_txns_src_22000.account_id = subq_30.account_id - ) AND ( - DATE_TRUNC('day', account_month_txns_src_22000.ds_partitioned) = subq_30.ds_partitioned__day - ) - GROUP BY - subq_30.customer_id__customer_third_hop_id - ) subq_35 + bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id + ) subq_30 ON - third_hop_table_src_22000.customer_third_hop_id = subq_35.account_id__customer_id__customer_third_hop_id -) subq_36 -WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 + ( + account_month_txns_src_22000.account_id = subq_30.account_id + ) AND ( + DATE_TRUNC('day', account_month_txns_src_22000.ds_partitioned) = subq_30.ds_partitioned__day + ) + GROUP BY + subq_30.customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.account_id__customer_id__customer_third_hop_id + ) subq_36 + WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_inner_query_single_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_inner_query_single_hop__plan0_optimized.sql index b79ca66b2..ec5eacc32 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_inner_query_single_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_inner_query_single_hop__plan0_optimized.sql @@ -4,41 +4,50 @@ docstring: Tests rendering for a metric filter using a one-hop join in the inner query. sql_engine: Snowflake --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + customer_id__customer_third_hop_id + , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers + FROM ( + -- Read Elements From Semantic Model 'customer_other_data' + -- Metric Time Dimension 'acquired_ds' + SELECT + customer_third_hop_id AS customer_id__customer_third_hop_id + , country AS customer_id__country + , 1 AS customers_with_other_data + FROM ***************************.customer_other_data customer_other_data_src_22000 + ) subq_16 + WHERE customer_id__country = 'paraguay' + GROUP BY + customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['customer_id__customer_third_hop_id', 'customer_id__customer_third_hop_id__paraguayan_customers'] + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs SELECT - customer_id__customer_third_hop_id - , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers - FROM ( - -- Read Elements From Semantic Model 'customer_other_data' - -- Metric Time Dimension 'acquired_ds' - SELECT - customer_third_hop_id AS customer_id__customer_third_hop_id - , country AS customer_id__country - , 1 AS customers_with_other_data - FROM ***************************.customer_other_data customer_other_data_src_22000 - ) subq_16 - WHERE customer_id__country = 'paraguay' - GROUP BY - customer_id__customer_third_hop_id - ) subq_21 - ON - third_hop_table_src_22000.customer_third_hop_id = subq_21.customer_id__customer_third_hop_id -) subq_22 -WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 + cm_3_cte.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.customer_id__customer_third_hop_id + ) subq_22 + WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_metric_filtered_by_itself__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_metric_filtered_by_itself__plan0_optimized.sql index 1d6bf9b6c..c2d19be94 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_metric_filtered_by_itself__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_metric_filtered_by_itself__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query for a metric that filters by the same metric. sql_engine: Snowflake --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookers AS listing__bookers - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - listing_id AS listing - , guest_id AS bookers + cm_3_cte.listing__bookers AS listing__bookers + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookers > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.listing_id = cm_3_cte.listing + ) subq_20 + WHERE listing__bookers > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_metric_with_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_metric_with_metric_in_where_filter__plan0_optimized.sql index 0d5d30e3f..e6dd287e2 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_metric_with_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_metric_with_metric_in_where_filter__plan0_optimized.sql @@ -4,50 +4,60 @@ docstring: Tests a query with a metric in the metric-level where filter. sql_engine: Snowflake --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings', 'metric_time__day'] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - metric_time__day - , SUM(listings) AS active_listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.metric_time__day AS metric_time__day - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT - DATE_TRUNC('day', created_at) AS metric_time__day - , listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(listings) AS active_listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.metric_time__day AS metric_time__day + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT - listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 -GROUP BY - metric_time__day + DATE_TRUNC('day', created_at) AS metric_time__day + , listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 + GROUP BY + metric_time__day +) + +SELECT + metric_time__day AS metric_time__day + , active_listings AS active_listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql index fab73b5c9..7737d0bb9 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql @@ -6,40 +6,49 @@ docstring: Note this cumulative metric has no window / grain to date. sql_engine: Snowflake --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'revenue' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['txn_revenue', 'user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + user_id AS user + , SUM(revenue) AS user__revenue_all_time + FROM ***************************.fct_revenue revenue_src_28000 + GROUP BY + user_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.user__revenue_all_time AS user__revenue_all_time - , subq_13.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'revenue' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['txn_revenue', 'user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['user', 'user__revenue_all_time'] - SELECT - user_id AS user - , SUM(revenue) AS user__revenue_all_time - FROM ***************************.fct_revenue revenue_src_28000 - GROUP BY - user_id - ) subq_19 - ON - subq_13.user = subq_19.user -) subq_20 -WHERE user__revenue_all_time > 1 + cm_3_cte.user__revenue_all_time AS user__revenue_all_time + , subq_13.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.user = cm_3_cte.user + ) subq_20 + WHERE user__revenue_all_time > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql index a372ba649..5fe4ecabc 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql @@ -4,75 +4,89 @@ docstring: Tests a query with a derived metric in the query-level where filter. sql_engine: Snowflake --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_33.listing__views_times_booking_value AS listing__views_times_booking_value - , subq_20.listings AS listings + listing + , SUM(views) AS views FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'views_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['views', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__views_times_booking_value'] + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_28 + GROUP BY + listing +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing + , booking_value * views AS listing__views_times_booking_value + FROM ( + -- Combine Aggregated Outputs SELECT - listing - , booking_value * views AS listing__views_times_booking_value + COALESCE(cm_5_cte.listing, cm_6_cte.listing) AS listing + , MAX(cm_5_cte.booking_value) AS booking_value + , MAX(cm_6_cte.views) AS views + FROM cm_5_cte cm_5_cte + FULL OUTER JOIN + cm_6_cte cm_6_cte + ON + cm_5_cte.listing = cm_6_cte.listing + GROUP BY + COALESCE(cm_5_cte.listing, cm_6_cte.listing) + ) subq_31 +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_7_cte.listing__views_times_booking_value AS listing__views_times_booking_value + , subq_20.listings AS listings FROM ( - -- Combine Aggregated Outputs + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' SELECT - COALESCE(subq_25.listing, subq_30.listing) AS listing - , MAX(subq_25.booking_value) AS booking_value - , MAX(subq_30.views) AS views - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing_id AS listing - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_25 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing - , SUM(views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['views', 'listing'] - SELECT - listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_28 - GROUP BY - listing - ) subq_30 - ON - subq_25.listing = subq_30.listing - GROUP BY - COALESCE(subq_25.listing, subq_30.listing) - ) subq_31 - ) subq_33 - ON - subq_20.listing = subq_33.listing -) subq_34 -WHERE listing__views_times_booking_value > 1 + listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_7_cte cm_7_cte + ON + subq_20.listing = cm_7_cte.listing + ) subq_34 + WHERE listing__views_times_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql index 813fadcf9..2552bc6c6 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql @@ -4,62 +4,73 @@ docstring: Tests a query with 2 simple metrics in the query-level where filter. sql_engine: Snowflake --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_6 +WITH cm_4_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_25.listing__bookings AS listing__bookings - , subq_31.listing__bookers AS listing__bookers - , subq_19.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_19 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_22 + GROUP BY + listing +) + +, cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_4_cte.listing__bookings AS listing__bookings + , cm_5_cte.listing__bookers AS listing__bookers + , subq_19.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_22 - GROUP BY - listing - ) subq_25 - ON - subq_19.listing = subq_25.listing - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_31 - ON - subq_19.listing = subq_31.listing -) subq_32 -WHERE listing__bookings > 2 AND listing__bookers > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_19 + LEFT OUTER JOIN + cm_4_cte cm_4_cte + ON + subq_19.listing = cm_4_cte.listing + LEFT OUTER JOIN + cm_5_cte cm_5_cte + ON + subq_19.listing = cm_5_cte.listing + ) subq_32 + WHERE listing__bookings > 2 AND listing__bookers > 1 +) + +SELECT + listings AS listings +FROM cm_6_cte cm_6_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql index e8b9b0ede..19e2f381c 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql @@ -4,46 +4,65 @@ docstring: Tests a query with a ratio metric in the query-level where filter. sql_engine: Snowflake --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_9 +WITH cm_7_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - CAST(subq_25.bookings AS DOUBLE) / CAST(NULLIF(subq_25.bookers, 0) AS DOUBLE) AS listing__bookings_per_booker - , subq_20.listings AS listings + listing + , SUM(bookings) AS bookings + , COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + , 1 AS bookings + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_23 + GROUP BY + listing +) + +, cm_8_cte AS ( + -- Read From CTE For node_id=cm_7 + -- Compute Metrics via Expressions + SELECT + listing + , CAST(bookings AS DOUBLE) / CAST(NULLIF(bookers, 0) AS DOUBLE) AS listing__bookings_per_booker + FROM cm_7_cte cm_7_cte +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS bookings - , COUNT(DISTINCT bookers) AS bookers + cm_8_cte.listing__bookings_per_booker AS listing__bookings_per_booker + , subq_20.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - GROUP BY - listing - ) subq_25 - ON - subq_20.listing = subq_25.listing -) subq_28 -WHERE listing__bookings_per_booker > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_8_cte cm_8_cte + ON + subq_20.listing = cm_8_cte.listing + ) subq_28 + WHERE listing__bookings_per_booker > 1 +) + +SELECT + listings AS listings +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql index d2803d2c3..386a55f6f 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql @@ -4,45 +4,54 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: Snowflake --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql index f1d00ba85..3a935e1e3 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: Trino --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'guest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + guest_id AS guest + , SUM(booking_value) AS guest__booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + guest_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.guest__booking_value AS guest__booking_value - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - guest_id AS guest - , guest_id AS bookers + cm_3_cte.guest__booking_value AS guest__booking_value + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'guest'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['guest', 'guest__booking_value'] - SELECT - guest_id AS guest - , SUM(booking_value) AS guest__booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - guest_id - ) subq_19 - ON - subq_13.guest = subq_19.guest -) subq_20 -WHERE guest__booking_value > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.guest_id = cm_3_cte.guest + ) subq_20 + WHERE guest__booking_value > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_filter_with_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_filter_with_conversion_metric__plan0_optimized.sql index a2f8c8563..e5f6c52dd 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_filter_with_conversion_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_filter_with_conversion_metric__plan0_optimized.sql @@ -2,26 +2,13 @@ test_name: test_filter_with_conversion_metric test_filename: test_metric_filter_rendering.py sql_engine: Trino --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Compute Metrics via Expressions SELECT - CAST(subq_39.buys AS DOUBLE) / CAST(NULLIF(subq_39.visits, 0) AS DOUBLE) AS user__visit_buy_conversion_rate - , subq_24.listings AS listings + subq_39.user + , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS user__visit_buy_conversion_rate FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_24 - LEFT OUTER JOIN ( -- Combine Aggregated Outputs SELECT COALESCE(subq_28.user, subq_38.user) AS user @@ -116,7 +103,36 @@ FROM ( GROUP BY COALESCE(subq_28.user, subq_38.user) ) subq_39 - ON - subq_24.user = subq_39.user -) subq_42 -WHERE user__visit_buy_conversion_rate > 2 +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.user__visit_buy_conversion_rate AS user__visit_buy_conversion_rate + , subq_24.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_24 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_24.user = cm_3_cte.user + ) subq_42 + WHERE user__visit_buy_conversion_rate > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_group_by_has_local_entity_prefix__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_group_by_has_local_entity_prefix__plan0_optimized.sql index 380085c35..24726788f 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_group_by_has_local_entity_prefix__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_group_by_has_local_entity_prefix__plan0_optimized.sql @@ -2,43 +2,52 @@ test_name: test_group_by_has_local_entity_prefix test_filename: test_metric_filter_rendering.py sql_engine: Trino --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['average_booking_value', 'listing__user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listings_latest_src_28000.user_id AS listing__user + , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id + GROUP BY + listings_latest_src_28000.user_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_27.listing__user__average_booking_value AS user__listing__user__average_booking_value - , subq_17.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_17 - LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['average_booking_value', 'listing__user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing__user', 'listing__user__average_booking_value'] SELECT - listings_latest_src_28000.user_id AS listing__user - , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + cm_3_cte.listing__user__average_booking_value AS user__listing__user__average_booking_value + , subq_17.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_17 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + cm_3_cte cm_3_cte ON - bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id - GROUP BY - listings_latest_src_28000.user_id - ) subq_27 - ON - subq_17.user = subq_27.listing__user -) subq_28 -WHERE user__listing__user__average_booking_value > 1 + subq_17.user = cm_3_cte.listing__user + ) subq_28 + WHERE user__listing__user__average_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_inner_query_multi_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_inner_query_multi_hop__plan0_optimized.sql index e44112b9f..8d1244e1b 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_inner_query_multi_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_inner_query_multi_hop__plan0_optimized.sql @@ -4,51 +4,60 @@ docstring: Tests rendering for a metric filter using a two-hop join in the inner query. sql_engine: Trino --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_35.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 + subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id + , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count + FROM ***************************.account_month_txns account_month_txns_src_22000 LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['account_id__customer_id__customer_third_hop_id', 'account_id__customer_id__customer_third_hop_id__txn_count'] + -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] SELECT - subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id - , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count - FROM ***************************.account_month_txns account_month_txns_src_22000 - LEFT OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] - SELECT - DATE_TRUNC('day', bridge_table_src_22000.ds_partitioned) AS ds_partitioned__day - , bridge_table_src_22000.account_id AS account_id - , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id - FROM ***************************.bridge_table bridge_table_src_22000 - LEFT OUTER JOIN - ***************************.customer_other_data customer_other_data_src_22000 - ON - bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id - ) subq_30 + DATE_TRUNC('day', bridge_table_src_22000.ds_partitioned) AS ds_partitioned__day + , bridge_table_src_22000.account_id AS account_id + , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id + FROM ***************************.bridge_table bridge_table_src_22000 + LEFT OUTER JOIN + ***************************.customer_other_data customer_other_data_src_22000 ON - ( - account_month_txns_src_22000.account_id = subq_30.account_id - ) AND ( - DATE_TRUNC('day', account_month_txns_src_22000.ds_partitioned) = subq_30.ds_partitioned__day - ) - GROUP BY - subq_30.customer_id__customer_third_hop_id - ) subq_35 + bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id + ) subq_30 ON - third_hop_table_src_22000.customer_third_hop_id = subq_35.account_id__customer_id__customer_third_hop_id -) subq_36 -WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 + ( + account_month_txns_src_22000.account_id = subq_30.account_id + ) AND ( + DATE_TRUNC('day', account_month_txns_src_22000.ds_partitioned) = subq_30.ds_partitioned__day + ) + GROUP BY + subq_30.customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.account_id__customer_id__customer_third_hop_id + ) subq_36 + WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_inner_query_single_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_inner_query_single_hop__plan0_optimized.sql index 6b05cc265..e81da75e6 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_inner_query_single_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_inner_query_single_hop__plan0_optimized.sql @@ -4,41 +4,50 @@ docstring: Tests rendering for a metric filter using a one-hop join in the inner query. sql_engine: Trino --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + customer_id__customer_third_hop_id + , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers + FROM ( + -- Read Elements From Semantic Model 'customer_other_data' + -- Metric Time Dimension 'acquired_ds' + SELECT + customer_third_hop_id AS customer_id__customer_third_hop_id + , country AS customer_id__country + , 1 AS customers_with_other_data + FROM ***************************.customer_other_data customer_other_data_src_22000 + ) subq_16 + WHERE customer_id__country = 'paraguay' + GROUP BY + customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['customer_id__customer_third_hop_id', 'customer_id__customer_third_hop_id__paraguayan_customers'] + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs SELECT - customer_id__customer_third_hop_id - , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers - FROM ( - -- Read Elements From Semantic Model 'customer_other_data' - -- Metric Time Dimension 'acquired_ds' - SELECT - customer_third_hop_id AS customer_id__customer_third_hop_id - , country AS customer_id__country - , 1 AS customers_with_other_data - FROM ***************************.customer_other_data customer_other_data_src_22000 - ) subq_16 - WHERE customer_id__country = 'paraguay' - GROUP BY - customer_id__customer_third_hop_id - ) subq_21 - ON - third_hop_table_src_22000.customer_third_hop_id = subq_21.customer_id__customer_third_hop_id -) subq_22 -WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 + cm_3_cte.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.customer_id__customer_third_hop_id + ) subq_22 + WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_metric_filtered_by_itself__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_metric_filtered_by_itself__plan0_optimized.sql index 2a95d127c..8fec6d158 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_metric_filtered_by_itself__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_metric_filtered_by_itself__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query for a metric that filters by the same metric. sql_engine: Trino --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookers AS listing__bookers - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - listing_id AS listing - , guest_id AS bookers + cm_3_cte.listing__bookers AS listing__bookers + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookers > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.listing_id = cm_3_cte.listing + ) subq_20 + WHERE listing__bookers > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_metric_with_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_metric_with_metric_in_where_filter__plan0_optimized.sql index 9863eed9d..c148be777 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_metric_with_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_metric_with_metric_in_where_filter__plan0_optimized.sql @@ -4,50 +4,60 @@ docstring: Tests a query with a metric in the metric-level where filter. sql_engine: Trino --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings', 'metric_time__day'] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - metric_time__day - , SUM(listings) AS active_listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.metric_time__day AS metric_time__day - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT - DATE_TRUNC('day', created_at) AS metric_time__day - , listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(listings) AS active_listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.metric_time__day AS metric_time__day + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT - listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 -GROUP BY - metric_time__day + DATE_TRUNC('day', created_at) AS metric_time__day + , listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 + GROUP BY + metric_time__day +) + +SELECT + metric_time__day AS metric_time__day + , active_listings AS active_listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql index 530cc6437..f126461cf 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql @@ -6,40 +6,49 @@ docstring: Note this cumulative metric has no window / grain to date. sql_engine: Trino --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'revenue' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['txn_revenue', 'user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + user_id AS user + , SUM(revenue) AS user__revenue_all_time + FROM ***************************.fct_revenue revenue_src_28000 + GROUP BY + user_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.user__revenue_all_time AS user__revenue_all_time - , subq_13.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'revenue' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['txn_revenue', 'user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['user', 'user__revenue_all_time'] - SELECT - user_id AS user - , SUM(revenue) AS user__revenue_all_time - FROM ***************************.fct_revenue revenue_src_28000 - GROUP BY - user_id - ) subq_19 - ON - subq_13.user = subq_19.user -) subq_20 -WHERE user__revenue_all_time > 1 + cm_3_cte.user__revenue_all_time AS user__revenue_all_time + , subq_13.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.user = cm_3_cte.user + ) subq_20 + WHERE user__revenue_all_time > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql index fedb76229..db6cf323b 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql @@ -4,75 +4,89 @@ docstring: Tests a query with a derived metric in the query-level where filter. sql_engine: Trino --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_33.listing__views_times_booking_value AS listing__views_times_booking_value - , subq_20.listings AS listings + listing + , SUM(views) AS views FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'views_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['views', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__views_times_booking_value'] + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_28 + GROUP BY + listing +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing + , booking_value * views AS listing__views_times_booking_value + FROM ( + -- Combine Aggregated Outputs SELECT - listing - , booking_value * views AS listing__views_times_booking_value + COALESCE(cm_5_cte.listing, cm_6_cte.listing) AS listing + , MAX(cm_5_cte.booking_value) AS booking_value + , MAX(cm_6_cte.views) AS views + FROM cm_5_cte cm_5_cte + FULL OUTER JOIN + cm_6_cte cm_6_cte + ON + cm_5_cte.listing = cm_6_cte.listing + GROUP BY + COALESCE(cm_5_cte.listing, cm_6_cte.listing) + ) subq_31 +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_7_cte.listing__views_times_booking_value AS listing__views_times_booking_value + , subq_20.listings AS listings FROM ( - -- Combine Aggregated Outputs + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' SELECT - COALESCE(subq_25.listing, subq_30.listing) AS listing - , MAX(subq_25.booking_value) AS booking_value - , MAX(subq_30.views) AS views - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing_id AS listing - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_25 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing - , SUM(views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['views', 'listing'] - SELECT - listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_28 - GROUP BY - listing - ) subq_30 - ON - subq_25.listing = subq_30.listing - GROUP BY - COALESCE(subq_25.listing, subq_30.listing) - ) subq_31 - ) subq_33 - ON - subq_20.listing = subq_33.listing -) subq_34 -WHERE listing__views_times_booking_value > 1 + listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_7_cte cm_7_cte + ON + subq_20.listing = cm_7_cte.listing + ) subq_34 + WHERE listing__views_times_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql index 0d390ad77..47b2bf7ed 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql @@ -4,62 +4,73 @@ docstring: Tests a query with 2 simple metrics in the query-level where filter. sql_engine: Trino --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_6 +WITH cm_4_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_25.listing__bookings AS listing__bookings - , subq_31.listing__bookers AS listing__bookers - , subq_19.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_19 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_22 + GROUP BY + listing +) + +, cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_4_cte.listing__bookings AS listing__bookings + , cm_5_cte.listing__bookers AS listing__bookers + , subq_19.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_22 - GROUP BY - listing - ) subq_25 - ON - subq_19.listing = subq_25.listing - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_31 - ON - subq_19.listing = subq_31.listing -) subq_32 -WHERE listing__bookings > 2 AND listing__bookers > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_19 + LEFT OUTER JOIN + cm_4_cte cm_4_cte + ON + subq_19.listing = cm_4_cte.listing + LEFT OUTER JOIN + cm_5_cte cm_5_cte + ON + subq_19.listing = cm_5_cte.listing + ) subq_32 + WHERE listing__bookings > 2 AND listing__bookers > 1 +) + +SELECT + listings AS listings +FROM cm_6_cte cm_6_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql index de93123c5..73e29727a 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql @@ -4,46 +4,65 @@ docstring: Tests a query with a ratio metric in the query-level where filter. sql_engine: Trino --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_9 +WITH cm_7_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - CAST(subq_25.bookings AS DOUBLE) / CAST(NULLIF(subq_25.bookers, 0) AS DOUBLE) AS listing__bookings_per_booker - , subq_20.listings AS listings + listing + , SUM(bookings) AS bookings + , COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + , 1 AS bookings + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_23 + GROUP BY + listing +) + +, cm_8_cte AS ( + -- Read From CTE For node_id=cm_7 + -- Compute Metrics via Expressions + SELECT + listing + , CAST(bookings AS DOUBLE) / CAST(NULLIF(bookers, 0) AS DOUBLE) AS listing__bookings_per_booker + FROM cm_7_cte cm_7_cte +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS bookings - , COUNT(DISTINCT bookers) AS bookers + cm_8_cte.listing__bookings_per_booker AS listing__bookings_per_booker + , subq_20.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - GROUP BY - listing - ) subq_25 - ON - subq_20.listing = subq_25.listing -) subq_28 -WHERE listing__bookings_per_booker > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_8_cte cm_8_cte + ON + subq_20.listing = cm_8_cte.listing + ) subq_28 + WHERE listing__bookings_per_booker > 1 +) + +SELECT + listings AS listings +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql index 8752d079c..275cd77a3 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql @@ -4,45 +4,54 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: Trino --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/BigQuery/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/BigQuery/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index 0b4cc0bff..becd9fa00 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/BigQuery/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/BigQuery/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -15,11 +15,7 @@ WITH rss_28020_cte AS ( FROM ***************************.fct_bookings bookings_source_src_28000 ) -SELECT - COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) AS metric_time__day - , MAX(subq_14.bookings) AS bookings - , MAX(subq_19.booking_payments) AS booking_payments -FROM ( +, cm_0_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] @@ -31,8 +27,9 @@ FROM ( FROM rss_28020_cte rss_28020_cte GROUP BY metric_time__day -) subq_14 -FULL OUTER JOIN ( +) + +, cm_1_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'paid_at' -- Pass Only Elements: ['booking_payments', 'metric_time__day'] @@ -44,8 +41,16 @@ FULL OUTER JOIN ( FROM rss_28020_cte rss_28020_cte GROUP BY metric_time__day -) subq_19 +) + +SELECT + COALESCE(cm_0_cte.metric_time__day, cm_1_cte.metric_time__day) AS metric_time__day + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.booking_payments) AS booking_payments +FROM cm_0_cte cm_0_cte +FULL OUTER JOIN + cm_1_cte cm_1_cte ON - subq_14.metric_time__day = subq_19.metric_time__day + cm_0_cte.metric_time__day = cm_1_cte.metric_time__day GROUP BY metric_time__day diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Databricks/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Databricks/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index 4a9171736..44bb3aa0b 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Databricks/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Databricks/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -15,11 +15,7 @@ WITH rss_28020_cte AS ( FROM ***************************.fct_bookings bookings_source_src_28000 ) -SELECT - COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) AS metric_time__day - , MAX(subq_14.bookings) AS bookings - , MAX(subq_19.booking_payments) AS booking_payments -FROM ( +, cm_0_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] @@ -31,8 +27,9 @@ FROM ( FROM rss_28020_cte rss_28020_cte GROUP BY ds__day -) subq_14 -FULL OUTER JOIN ( +) + +, cm_1_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'paid_at' -- Pass Only Elements: ['booking_payments', 'metric_time__day'] @@ -44,8 +41,16 @@ FULL OUTER JOIN ( FROM rss_28020_cte rss_28020_cte GROUP BY paid_at__day -) subq_19 +) + +SELECT + COALESCE(cm_0_cte.metric_time__day, cm_1_cte.metric_time__day) AS metric_time__day + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.booking_payments) AS booking_payments +FROM cm_0_cte cm_0_cte +FULL OUTER JOIN + cm_1_cte cm_1_cte ON - subq_14.metric_time__day = subq_19.metric_time__day + cm_0_cte.metric_time__day = cm_1_cte.metric_time__day GROUP BY - COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) + COALESCE(cm_0_cte.metric_time__day, cm_1_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Postgres/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Postgres/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index 60600f154..a3f15fdae 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Postgres/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Postgres/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -15,11 +15,7 @@ WITH rss_28020_cte AS ( FROM ***************************.fct_bookings bookings_source_src_28000 ) -SELECT - COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) AS metric_time__day - , MAX(subq_14.bookings) AS bookings - , MAX(subq_19.booking_payments) AS booking_payments -FROM ( +, cm_0_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] @@ -31,8 +27,9 @@ FROM ( FROM rss_28020_cte rss_28020_cte GROUP BY ds__day -) subq_14 -FULL OUTER JOIN ( +) + +, cm_1_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'paid_at' -- Pass Only Elements: ['booking_payments', 'metric_time__day'] @@ -44,8 +41,16 @@ FULL OUTER JOIN ( FROM rss_28020_cte rss_28020_cte GROUP BY paid_at__day -) subq_19 +) + +SELECT + COALESCE(cm_0_cte.metric_time__day, cm_1_cte.metric_time__day) AS metric_time__day + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.booking_payments) AS booking_payments +FROM cm_0_cte cm_0_cte +FULL OUTER JOIN + cm_1_cte cm_1_cte ON - subq_14.metric_time__day = subq_19.metric_time__day + cm_0_cte.metric_time__day = cm_1_cte.metric_time__day GROUP BY - COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) + COALESCE(cm_0_cte.metric_time__day, cm_1_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Redshift/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Redshift/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index cb4519e9f..4abb45763 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Redshift/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Redshift/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -15,11 +15,7 @@ WITH rss_28020_cte AS ( FROM ***************************.fct_bookings bookings_source_src_28000 ) -SELECT - COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) AS metric_time__day - , MAX(subq_14.bookings) AS bookings - , MAX(subq_19.booking_payments) AS booking_payments -FROM ( +, cm_0_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] @@ -31,8 +27,9 @@ FROM ( FROM rss_28020_cte rss_28020_cte GROUP BY ds__day -) subq_14 -FULL OUTER JOIN ( +) + +, cm_1_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'paid_at' -- Pass Only Elements: ['booking_payments', 'metric_time__day'] @@ -44,8 +41,16 @@ FULL OUTER JOIN ( FROM rss_28020_cte rss_28020_cte GROUP BY paid_at__day -) subq_19 +) + +SELECT + COALESCE(cm_0_cte.metric_time__day, cm_1_cte.metric_time__day) AS metric_time__day + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.booking_payments) AS booking_payments +FROM cm_0_cte cm_0_cte +FULL OUTER JOIN + cm_1_cte cm_1_cte ON - subq_14.metric_time__day = subq_19.metric_time__day + cm_0_cte.metric_time__day = cm_1_cte.metric_time__day GROUP BY - COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) + COALESCE(cm_0_cte.metric_time__day, cm_1_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Snowflake/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Snowflake/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index 477d340b0..cbc3ec60a 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Snowflake/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Snowflake/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -15,11 +15,7 @@ WITH rss_28020_cte AS ( FROM ***************************.fct_bookings bookings_source_src_28000 ) -SELECT - COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) AS metric_time__day - , MAX(subq_14.bookings) AS bookings - , MAX(subq_19.booking_payments) AS booking_payments -FROM ( +, cm_0_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] @@ -31,8 +27,9 @@ FROM ( FROM rss_28020_cte rss_28020_cte GROUP BY ds__day -) subq_14 -FULL OUTER JOIN ( +) + +, cm_1_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'paid_at' -- Pass Only Elements: ['booking_payments', 'metric_time__day'] @@ -44,8 +41,16 @@ FULL OUTER JOIN ( FROM rss_28020_cte rss_28020_cte GROUP BY paid_at__day -) subq_19 +) + +SELECT + COALESCE(cm_0_cte.metric_time__day, cm_1_cte.metric_time__day) AS metric_time__day + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.booking_payments) AS booking_payments +FROM cm_0_cte cm_0_cte +FULL OUTER JOIN + cm_1_cte cm_1_cte ON - subq_14.metric_time__day = subq_19.metric_time__day + cm_0_cte.metric_time__day = cm_1_cte.metric_time__day GROUP BY - COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) + COALESCE(cm_0_cte.metric_time__day, cm_1_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Trino/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Trino/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index afb011f5d..c1a3ecf10 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Trino/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Trino/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -15,11 +15,7 @@ WITH rss_28020_cte AS ( FROM ***************************.fct_bookings bookings_source_src_28000 ) -SELECT - COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) AS metric_time__day - , MAX(subq_14.bookings) AS bookings - , MAX(subq_19.booking_payments) AS booking_payments -FROM ( +, cm_0_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] @@ -31,8 +27,9 @@ FROM ( FROM rss_28020_cte rss_28020_cte GROUP BY ds__day -) subq_14 -FULL OUTER JOIN ( +) + +, cm_1_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'paid_at' -- Pass Only Elements: ['booking_payments', 'metric_time__day'] @@ -44,8 +41,16 @@ FULL OUTER JOIN ( FROM rss_28020_cte rss_28020_cte GROUP BY paid_at__day -) subq_19 +) + +SELECT + COALESCE(cm_0_cte.metric_time__day, cm_1_cte.metric_time__day) AS metric_time__day + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.booking_payments) AS booking_payments +FROM cm_0_cte cm_0_cte +FULL OUTER JOIN + cm_1_cte cm_1_cte ON - subq_14.metric_time__day = subq_19.metric_time__day + cm_0_cte.metric_time__day = cm_1_cte.metric_time__day GROUP BY - COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) + COALESCE(cm_0_cte.metric_time__day, cm_1_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql index 71d8872b7..27a2309f1 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql @@ -12,52 +12,65 @@ docstring: the source input for the latter input must NOT have the filter applied to it. sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(average_booking_value AS FLOAT64) / CAST(NULLIF(max_booking_value, 0) AS FLOAT64) AS instant_booking_fraction_of_max_value -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.average_booking_value) AS average_booking_value - , MAX(subq_22.max_booking_value) AS max_booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATETIME_TRUNC(ds, day) AS metric_time__day - , MAX(booking_value) AS max_booking_value + , is_instant AS booking__is_instant + , booking_value AS average_booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - metric_time__day - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant GROUP BY metric_time__day -) subq_23 +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , MAX(booking_value) AS max_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + metric_time__day +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(average_booking_value AS FLOAT64) / CAST(NULLIF(max_booking_value, 0) AS FLOAT64) AS instant_booking_fraction_of_max_value + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.average_booking_value) AS average_booking_value + , MAX(cm_7_cte.max_booking_value) AS max_booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_fraction_of_max_value AS instant_booking_fraction_of_max_value +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql index c0df4693c..6368e329a 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql @@ -6,47 +6,97 @@ docstring: TODO: support metric time filters sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest - , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_40.ds AS metric_time__day + , subq_38.listing__country_latest AS listing__country_latest + , subq_38.bookings AS bookings + FROM ***************************.mf_time_spine subq_40 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_31.metric_time__day AS metric_time__day + , subq_31.booking__is_instant AS booking__is_instant + , subq_31.bookings AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_31.listing = listings_latest_src_28000.listing_id + ) subq_35 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_38 + ON + subq_40.ds = subq_38.metric_time__day + ) subq_41 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_2_weeks_ago + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_56.ds AS metric_time__day + , subq_54.listing__country_latest AS listing__country_latest + , subq_54.bookings AS bookings + FROM ***************************.mf_time_spine subq_56 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures SELECT - subq_40.ds AS metric_time__day - , subq_38.listing__country_latest AS listing__country_latest - , subq_38.bookings AS bookings - FROM ***************************.mf_time_spine subq_40 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_47.metric_time__day AS metric_time__day + , subq_47.booking__is_instant AS booking__is_instant + , subq_47.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Join to Time Spine Dataset SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_31.metric_time__day AS metric_time__day - , subq_31.booking__is_instant AS booking__is_instant - , subq_31.bookings AS bookings - FROM ( + subq_46.ds AS metric_time__day + , subq_44.listing AS listing + , subq_44.booking__is_instant AS booking__is_instant + , subq_44.bookings AS bookings + FROM ***************************.mf_time_spine subq_46 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -55,91 +105,68 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_44 ON - subq_31.listing = listings_latest_src_28000.listing_id - ) subq_35 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_38 - ON - subq_40.ds = subq_38.metric_time__day - ) subq_41 - ) subq_42 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + DATE_SUB(CAST(subq_46.ds AS DATETIME), INTERVAL 14 day) = subq_44.metric_time__day + ) subq_47 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_47.listing = listings_latest_src_28000.listing_id + ) subq_51 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_54 + ON + subq_56.ds = subq_54.metric_time__day + ) subq_57 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_2_weeks_ago + COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day + , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest + , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_6 SELECT - subq_56.ds AS metric_time__day - , subq_54.listing__country_latest AS listing__country_latest - , subq_54.bookings AS bookings - FROM ***************************.mf_time_spine subq_56 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_47.metric_time__day AS metric_time__day - , subq_47.booking__is_instant AS booking__is_instant - , subq_47.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_46.ds AS metric_time__day - , subq_44.listing AS listing - , subq_44.booking__is_instant AS booking__is_instant - , subq_44.bookings AS bookings - FROM ***************************.mf_time_spine subq_46 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_44 - ON - DATE_SUB(CAST(subq_46.ds AS DATETIME), INTERVAL 14 day) = subq_44.metric_time__day - ) subq_47 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_47.listing = listings_latest_src_28000.listing_id - ) subq_51 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_54 - ON - subq_56.ds = subq_54.metric_time__day - ) subq_57 - ) subq_58 - ON - ( - subq_42.listing__country_latest = subq_58.listing__country_latest - ) AND ( - subq_42.metric_time__day = subq_58.metric_time__day - ) - GROUP BY - metric_time__day - , listing__country_latest -) subq_59 + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_42 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , listing__country_latest + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_58 + ON + ( + subq_42.listing__country_latest = subq_58.listing__country_latest + ) AND ( + subq_42.metric_time__day = subq_58.metric_time__day + ) + GROUP BY + metric_time__day + , listing__country_latest + ) subq_59 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks_fill_nulls_with_0 AS bookings_growth_2_weeks_fill_nulls_with_0 +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_offset_metric_with_query_time_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_offset_metric_with_query_time_filters__plan0_optimized.sql index 649c63350..9ac3203b5 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_offset_metric_with_query_time_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_offset_metric_with_query_time_filters__plan0_optimized.sql @@ -6,35 +6,69 @@ docstring: TODO: support metric time filters sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) AS metric_time__day - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) AS listing__country_latest - , MAX(subq_33.bookings) AS bookings - , MAX(subq_46.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_25.metric_time__day AS metric_time__day + , subq_25.booking__is_instant AS booking__is_instant + , subq_25.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_25.metric_time__day AS metric_time__day - , subq_25.booking__is_instant AS booking__is_instant - , subq_25.bookings AS bookings - FROM ( + DATETIME_TRUNC(ds, day) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_25.listing = listings_latest_src_28000.listing_id + ) subq_29 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings_2_weeks_ago + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_38.metric_time__day AS metric_time__day + , subq_38.booking__is_instant AS booking__is_instant + , subq_38.bookings AS bookings + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_37.ds AS metric_time__day + , subq_35.listing AS listing + , subq_35.booking__is_instant AS booking__is_instant + , subq_35.bookings AS bookings + FROM ***************************.mf_time_spine subq_37 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -43,71 +77,51 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_35 ON - subq_25.listing = listings_latest_src_28000.listing_id - ) subq_29 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_33 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_SUB(CAST(subq_37.ds AS DATETIME), INTERVAL 14 day) = subq_35.metric_time__day + ) subq_38 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_38.listing = listings_latest_src_28000.listing_id + ) subq_42 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_38.metric_time__day AS metric_time__day - , subq_38.booking__is_instant AS booking__is_instant - , subq_38.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_37.ds AS metric_time__day - , subq_35.listing AS listing - , subq_35.booking__is_instant AS booking__is_instant - , subq_35.bookings AS bookings - FROM ***************************.mf_time_spine subq_37 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_35 - ON - DATE_SUB(CAST(subq_37.ds AS DATETIME), INTERVAL 14 day) = subq_35.metric_time__day - ) subq_38 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_38.listing = listings_latest_src_28000.listing_id - ) subq_42 - WHERE booking__is_instant + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.listing__country_latest, cm_7_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + ( + cm_6_cte.listing__country_latest = cm_7_cte.listing__country_latest + ) AND ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) GROUP BY metric_time__day , listing__country_latest - ) subq_46 - ON - ( - subq_33.listing__country_latest = subq_46.listing__country_latest - ) AND ( - subq_33.metric_time__day = subq_46.metric_time__day - ) - GROUP BY - metric_time__day - , listing__country_latest -) subq_47 + ) subq_47 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql index be3f4b446..9fa671c44 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql @@ -7,12 +7,7 @@ docstring: sql_engine: BigQuery --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_51.bookings) AS bookings - , MAX(subq_61.views) AS views - , MAX(CAST(subq_82.bookings AS FLOAT64) / CAST(NULLIF(subq_82.views, 0) AS FLOAT64)) AS bookings_per_view -FROM ( +WITH cm_6_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] -- Aggregate Measures @@ -44,8 +39,9 @@ FROM ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_51 -FULL OUTER JOIN ( +) + +, cm_7_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['views', 'listing__capacity_latest'] -- Aggregate Measures @@ -77,87 +73,110 @@ FULL OUTER JOIN ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_61 -ON - subq_51.listing__capacity_latest = subq_61.listing__capacity_latest -FULL OUTER JOIN ( - -- Combine Aggregated Outputs +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_71.bookings) AS bookings - , MAX(subq_81.views) AS views + listing__capacity_latest + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_63.metric_time__day AS metric_time__day + , subq_63.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_63.metric_time__day AS metric_time__day - , subq_63.bookings AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_63 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_63.listing = listings_latest_src_28000.listing_id - ) subq_67 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_71 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['views', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATETIME_TRUNC(ds, day) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_63 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_63.listing = listings_latest_src_28000.listing_id + ) subq_67 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') + GROUP BY + listing__capacity_latest +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['views', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , SUM(views) AS views + FROM ( + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(views) AS views + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_73.metric_time__day AS metric_time__day + , subq_73.views AS views FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_73.metric_time__day AS metric_time__day - , subq_73.views AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_73 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_73.listing = listings_latest_src_28000.listing_id - ) subq_77 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_81 - ON - subq_71.listing__capacity_latest = subq_81.listing__capacity_latest + DATETIME_TRUNC(ds, day) AS metric_time__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_73 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_73.listing = listings_latest_src_28000.listing_id + ) subq_77 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_82 +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , CAST(bookings AS FLOAT64) / CAST(NULLIF(views, 0) AS FLOAT64) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_8_cte.listing__capacity_latest, cm_9_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_8_cte.bookings) AS bookings + , MAX(cm_9_cte.views) AS views + FROM cm_8_cte cm_8_cte + FULL OUTER JOIN + cm_9_cte cm_9_cte + ON + cm_8_cte.listing__capacity_latest = cm_9_cte.listing__capacity_latest + GROUP BY + listing__capacity_latest + ) subq_82 +) + +SELECT + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, cm_10_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.views) AS views + , MAX(cm_10_cte.bookings_per_view) AS bookings_per_view +FROM cm_6_cte cm_6_cte +FULL OUTER JOIN + cm_7_cte cm_7_cte +ON + cm_6_cte.listing__capacity_latest = cm_7_cte.listing__capacity_latest +FULL OUTER JOIN + cm_10_cte cm_10_cte ON - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest) = subq_82.listing__capacity_latest + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest) = cm_10_cte.listing__capacity_latest GROUP BY listing__capacity_latest diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql index 92979c007..0d6fa19c3 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql @@ -12,52 +12,65 @@ docstring: the source input for the latter input must NOT have the filter applied to it. sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(average_booking_value AS DOUBLE) / CAST(NULLIF(max_booking_value, 0) AS DOUBLE) AS instant_booking_fraction_of_max_value -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.average_booking_value) AS average_booking_value - , MAX(subq_22.max_booking_value) AS max_booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATE_TRUNC('day', ds) AS metric_time__day - , MAX(booking_value) AS max_booking_value + , is_instant AS booking__is_instant + , booking_value AS average_booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , MAX(booking_value) AS max_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 GROUP BY - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) -) subq_23 + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(average_booking_value AS DOUBLE) / CAST(NULLIF(max_booking_value, 0) AS DOUBLE) AS instant_booking_fraction_of_max_value + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.average_booking_value) AS average_booking_value + , MAX(cm_7_cte.max_booking_value) AS max_booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_fraction_of_max_value AS instant_booking_fraction_of_max_value +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql index 9daa5092a..a86cca091 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql @@ -6,47 +6,97 @@ docstring: TODO: support metric time filters sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest - , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_40.ds AS metric_time__day + , subq_38.listing__country_latest AS listing__country_latest + , subq_38.bookings AS bookings + FROM ***************************.mf_time_spine subq_40 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures SELECT - subq_40.ds AS metric_time__day - , subq_38.listing__country_latest AS listing__country_latest - , subq_38.bookings AS bookings - FROM ***************************.mf_time_spine subq_40 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_31.metric_time__day AS metric_time__day + , subq_31.booking__is_instant AS booking__is_instant + , subq_31.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_31.metric_time__day AS metric_time__day - , subq_31.booking__is_instant AS booking__is_instant - , subq_31.bookings AS bookings - FROM ( + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_31.listing = listings_latest_src_28000.listing_id + ) subq_35 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_38 + ON + subq_40.ds = subq_38.metric_time__day + ) subq_41 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_2_weeks_ago + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_56.ds AS metric_time__day + , subq_54.listing__country_latest AS listing__country_latest + , subq_54.bookings AS bookings + FROM ***************************.mf_time_spine subq_56 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_47.metric_time__day AS metric_time__day + , subq_47.booking__is_instant AS booking__is_instant + , subq_47.bookings AS bookings + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_46.ds AS metric_time__day + , subq_44.listing AS listing + , subq_44.booking__is_instant AS booking__is_instant + , subq_44.bookings AS bookings + FROM ***************************.mf_time_spine subq_46 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -55,91 +105,68 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_44 ON - subq_31.listing = listings_latest_src_28000.listing_id - ) subq_35 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_38 - ON - subq_40.ds = subq_38.metric_time__day - ) subq_41 - ) subq_42 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + DATEADD(day, -14, subq_46.ds) = subq_44.metric_time__day + ) subq_47 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_47.listing = listings_latest_src_28000.listing_id + ) subq_51 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_54 + ON + subq_56.ds = subq_54.metric_time__day + ) subq_57 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_2_weeks_ago + COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day + , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest + , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_6 SELECT - subq_56.ds AS metric_time__day - , subq_54.listing__country_latest AS listing__country_latest - , subq_54.bookings AS bookings - FROM ***************************.mf_time_spine subq_56 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_47.metric_time__day AS metric_time__day - , subq_47.booking__is_instant AS booking__is_instant - , subq_47.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_46.ds AS metric_time__day - , subq_44.listing AS listing - , subq_44.booking__is_instant AS booking__is_instant - , subq_44.bookings AS bookings - FROM ***************************.mf_time_spine subq_46 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_44 - ON - DATEADD(day, -14, subq_46.ds) = subq_44.metric_time__day - ) subq_47 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_47.listing = listings_latest_src_28000.listing_id - ) subq_51 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_54 - ON - subq_56.ds = subq_54.metric_time__day - ) subq_57 - ) subq_58 - ON - ( - subq_42.listing__country_latest = subq_58.listing__country_latest - ) AND ( - subq_42.metric_time__day = subq_58.metric_time__day - ) - GROUP BY - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) -) subq_59 + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_42 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , listing__country_latest + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_58 + ON + ( + subq_42.listing__country_latest = subq_58.listing__country_latest + ) AND ( + subq_42.metric_time__day = subq_58.metric_time__day + ) + GROUP BY + COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) + , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) + ) subq_59 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks_fill_nulls_with_0 AS bookings_growth_2_weeks_fill_nulls_with_0 +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_offset_metric_with_query_time_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_offset_metric_with_query_time_filters__plan0_optimized.sql index 3916fbc8d..394dd1880 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_offset_metric_with_query_time_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_offset_metric_with_query_time_filters__plan0_optimized.sql @@ -6,35 +6,69 @@ docstring: TODO: support metric time filters sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_25.metric_time__day AS metric_time__day + , subq_25.booking__is_instant AS booking__is_instant + , subq_25.bookings AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_25.listing = listings_latest_src_28000.listing_id + ) subq_29 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) AS metric_time__day - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) AS listing__country_latest - , MAX(subq_33.bookings) AS bookings - , MAX(subq_46.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings_2_weeks_ago FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_38.metric_time__day AS metric_time__day + , subq_38.booking__is_instant AS booking__is_instant + , subq_38.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Join to Time Spine Dataset SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_25.metric_time__day AS metric_time__day - , subq_25.booking__is_instant AS booking__is_instant - , subq_25.bookings AS bookings - FROM ( + subq_37.ds AS metric_time__day + , subq_35.listing AS listing + , subq_35.booking__is_instant AS booking__is_instant + , subq_35.bookings AS bookings + FROM ***************************.mf_time_spine subq_37 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -43,71 +77,51 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_35 ON - subq_25.listing = listings_latest_src_28000.listing_id - ) subq_29 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_33 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATEADD(day, -14, subq_37.ds) = subq_35.metric_time__day + ) subq_38 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_38.listing = listings_latest_src_28000.listing_id + ) subq_42 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_38.metric_time__day AS metric_time__day - , subq_38.booking__is_instant AS booking__is_instant - , subq_38.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_37.ds AS metric_time__day - , subq_35.listing AS listing - , subq_35.booking__is_instant AS booking__is_instant - , subq_35.bookings AS bookings - FROM ***************************.mf_time_spine subq_37 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_35 - ON - DATEADD(day, -14, subq_37.ds) = subq_35.metric_time__day - ) subq_38 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_38.listing = listings_latest_src_28000.listing_id - ) subq_42 - WHERE booking__is_instant + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.listing__country_latest, cm_7_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + ( + cm_6_cte.listing__country_latest = cm_7_cte.listing__country_latest + ) AND ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) GROUP BY - metric_time__day - , listing__country_latest - ) subq_46 - ON - ( - subq_33.listing__country_latest = subq_46.listing__country_latest - ) AND ( - subq_33.metric_time__day = subq_46.metric_time__day - ) - GROUP BY - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) -) subq_47 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + , COALESCE(cm_6_cte.listing__country_latest, cm_7_cte.listing__country_latest) + ) subq_47 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql index 605bb59d1..9cf6d1331 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql @@ -7,12 +7,7 @@ docstring: sql_engine: Databricks --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_51.bookings) AS bookings - , MAX(subq_61.views) AS views - , MAX(CAST(subq_82.bookings AS DOUBLE) / CAST(NULLIF(subq_82.views, 0) AS DOUBLE)) AS bookings_per_view -FROM ( +WITH cm_6_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] -- Aggregate Measures @@ -44,8 +39,9 @@ FROM ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_51 -FULL OUTER JOIN ( +) + +, cm_7_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['views', 'listing__capacity_latest'] -- Aggregate Measures @@ -77,87 +73,110 @@ FULL OUTER JOIN ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_61 -ON - subq_51.listing__capacity_latest = subq_61.listing__capacity_latest -FULL OUTER JOIN ( - -- Combine Aggregated Outputs +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_71.bookings) AS bookings - , MAX(subq_81.views) AS views + listing__capacity_latest + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_63.metric_time__day AS metric_time__day + , subq_63.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_63.metric_time__day AS metric_time__day - , subq_63.bookings AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_63 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_63.listing = listings_latest_src_28000.listing_id - ) subq_67 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_71 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['views', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_63 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_63.listing = listings_latest_src_28000.listing_id + ) subq_67 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') + GROUP BY + listing__capacity_latest +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['views', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , SUM(views) AS views + FROM ( + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(views) AS views + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_73.metric_time__day AS metric_time__day + , subq_73.views AS views FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_73.metric_time__day AS metric_time__day - , subq_73.views AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_73 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_73.listing = listings_latest_src_28000.listing_id - ) subq_77 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_81 - ON - subq_71.listing__capacity_latest = subq_81.listing__capacity_latest + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_73 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_73.listing = listings_latest_src_28000.listing_id + ) subq_77 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) -) subq_82 + listing__capacity_latest +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_8_cte.listing__capacity_latest, cm_9_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_8_cte.bookings) AS bookings + , MAX(cm_9_cte.views) AS views + FROM cm_8_cte cm_8_cte + FULL OUTER JOIN + cm_9_cte cm_9_cte + ON + cm_8_cte.listing__capacity_latest = cm_9_cte.listing__capacity_latest + GROUP BY + COALESCE(cm_8_cte.listing__capacity_latest, cm_9_cte.listing__capacity_latest) + ) subq_82 +) + +SELECT + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, cm_10_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.views) AS views + , MAX(cm_10_cte.bookings_per_view) AS bookings_per_view +FROM cm_6_cte cm_6_cte +FULL OUTER JOIN + cm_7_cte cm_7_cte +ON + cm_6_cte.listing__capacity_latest = cm_7_cte.listing__capacity_latest +FULL OUTER JOIN + cm_10_cte cm_10_cte ON - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest) = subq_82.listing__capacity_latest + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest) = cm_10_cte.listing__capacity_latest GROUP BY - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, cm_10_cte.listing__capacity_latest) diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql index 5fc69eaeb..82bb20018 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql @@ -12,52 +12,65 @@ docstring: the source input for the latter input must NOT have the filter applied to it. sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(average_booking_value AS DOUBLE PRECISION) / CAST(NULLIF(max_booking_value, 0) AS DOUBLE PRECISION) AS instant_booking_fraction_of_max_value -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.average_booking_value) AS average_booking_value - , MAX(subq_22.max_booking_value) AS max_booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATE_TRUNC('day', ds) AS metric_time__day - , MAX(booking_value) AS max_booking_value + , is_instant AS booking__is_instant + , booking_value AS average_booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , MAX(booking_value) AS max_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 GROUP BY - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) -) subq_23 + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(average_booking_value AS DOUBLE PRECISION) / CAST(NULLIF(max_booking_value, 0) AS DOUBLE PRECISION) AS instant_booking_fraction_of_max_value + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.average_booking_value) AS average_booking_value + , MAX(cm_7_cte.max_booking_value) AS max_booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_fraction_of_max_value AS instant_booking_fraction_of_max_value +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql index 15ac247a9..32364e7af 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql @@ -6,47 +6,97 @@ docstring: TODO: support metric time filters sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest - , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_40.ds AS metric_time__day + , subq_38.listing__country_latest AS listing__country_latest + , subq_38.bookings AS bookings + FROM ***************************.mf_time_spine subq_40 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures SELECT - subq_40.ds AS metric_time__day - , subq_38.listing__country_latest AS listing__country_latest - , subq_38.bookings AS bookings - FROM ***************************.mf_time_spine subq_40 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_31.metric_time__day AS metric_time__day + , subq_31.booking__is_instant AS booking__is_instant + , subq_31.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_31.metric_time__day AS metric_time__day - , subq_31.booking__is_instant AS booking__is_instant - , subq_31.bookings AS bookings - FROM ( + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_31.listing = listings_latest_src_28000.listing_id + ) subq_35 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_38 + ON + subq_40.ds = subq_38.metric_time__day + ) subq_41 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_2_weeks_ago + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_56.ds AS metric_time__day + , subq_54.listing__country_latest AS listing__country_latest + , subq_54.bookings AS bookings + FROM ***************************.mf_time_spine subq_56 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_47.metric_time__day AS metric_time__day + , subq_47.booking__is_instant AS booking__is_instant + , subq_47.bookings AS bookings + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_46.ds AS metric_time__day + , subq_44.listing AS listing + , subq_44.booking__is_instant AS booking__is_instant + , subq_44.bookings AS bookings + FROM ***************************.mf_time_spine subq_46 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -55,91 +105,68 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_44 ON - subq_31.listing = listings_latest_src_28000.listing_id - ) subq_35 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_38 - ON - subq_40.ds = subq_38.metric_time__day - ) subq_41 - ) subq_42 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + subq_46.ds - MAKE_INTERVAL(days => 14) = subq_44.metric_time__day + ) subq_47 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_47.listing = listings_latest_src_28000.listing_id + ) subq_51 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_54 + ON + subq_56.ds = subq_54.metric_time__day + ) subq_57 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_2_weeks_ago + COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day + , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest + , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_6 SELECT - subq_56.ds AS metric_time__day - , subq_54.listing__country_latest AS listing__country_latest - , subq_54.bookings AS bookings - FROM ***************************.mf_time_spine subq_56 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_47.metric_time__day AS metric_time__day - , subq_47.booking__is_instant AS booking__is_instant - , subq_47.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_46.ds AS metric_time__day - , subq_44.listing AS listing - , subq_44.booking__is_instant AS booking__is_instant - , subq_44.bookings AS bookings - FROM ***************************.mf_time_spine subq_46 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_44 - ON - subq_46.ds - MAKE_INTERVAL(days => 14) = subq_44.metric_time__day - ) subq_47 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_47.listing = listings_latest_src_28000.listing_id - ) subq_51 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_54 - ON - subq_56.ds = subq_54.metric_time__day - ) subq_57 - ) subq_58 - ON - ( - subq_42.listing__country_latest = subq_58.listing__country_latest - ) AND ( - subq_42.metric_time__day = subq_58.metric_time__day - ) - GROUP BY - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) -) subq_59 + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_42 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , listing__country_latest + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_58 + ON + ( + subq_42.listing__country_latest = subq_58.listing__country_latest + ) AND ( + subq_42.metric_time__day = subq_58.metric_time__day + ) + GROUP BY + COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) + , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) + ) subq_59 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks_fill_nulls_with_0 AS bookings_growth_2_weeks_fill_nulls_with_0 +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_offset_metric_with_query_time_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_offset_metric_with_query_time_filters__plan0_optimized.sql index 637b25a3f..6da79aa4b 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_offset_metric_with_query_time_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_offset_metric_with_query_time_filters__plan0_optimized.sql @@ -6,35 +6,69 @@ docstring: TODO: support metric time filters sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_25.metric_time__day AS metric_time__day + , subq_25.booking__is_instant AS booking__is_instant + , subq_25.bookings AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_25.listing = listings_latest_src_28000.listing_id + ) subq_29 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) AS metric_time__day - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) AS listing__country_latest - , MAX(subq_33.bookings) AS bookings - , MAX(subq_46.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings_2_weeks_ago FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_38.metric_time__day AS metric_time__day + , subq_38.booking__is_instant AS booking__is_instant + , subq_38.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Join to Time Spine Dataset SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_25.metric_time__day AS metric_time__day - , subq_25.booking__is_instant AS booking__is_instant - , subq_25.bookings AS bookings - FROM ( + subq_37.ds AS metric_time__day + , subq_35.listing AS listing + , subq_35.booking__is_instant AS booking__is_instant + , subq_35.bookings AS bookings + FROM ***************************.mf_time_spine subq_37 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -43,71 +77,51 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_35 ON - subq_25.listing = listings_latest_src_28000.listing_id - ) subq_29 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_33 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_37.ds - MAKE_INTERVAL(days => 14) = subq_35.metric_time__day + ) subq_38 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_38.listing = listings_latest_src_28000.listing_id + ) subq_42 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_38.metric_time__day AS metric_time__day - , subq_38.booking__is_instant AS booking__is_instant - , subq_38.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_37.ds AS metric_time__day - , subq_35.listing AS listing - , subq_35.booking__is_instant AS booking__is_instant - , subq_35.bookings AS bookings - FROM ***************************.mf_time_spine subq_37 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_35 - ON - subq_37.ds - MAKE_INTERVAL(days => 14) = subq_35.metric_time__day - ) subq_38 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_38.listing = listings_latest_src_28000.listing_id - ) subq_42 - WHERE booking__is_instant + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.listing__country_latest, cm_7_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + ( + cm_6_cte.listing__country_latest = cm_7_cte.listing__country_latest + ) AND ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) GROUP BY - metric_time__day - , listing__country_latest - ) subq_46 - ON - ( - subq_33.listing__country_latest = subq_46.listing__country_latest - ) AND ( - subq_33.metric_time__day = subq_46.metric_time__day - ) - GROUP BY - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) -) subq_47 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + , COALESCE(cm_6_cte.listing__country_latest, cm_7_cte.listing__country_latest) + ) subq_47 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql index c8ab77479..d78d9be6b 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql @@ -7,12 +7,7 @@ docstring: sql_engine: Postgres --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_51.bookings) AS bookings - , MAX(subq_61.views) AS views - , MAX(CAST(subq_82.bookings AS DOUBLE PRECISION) / CAST(NULLIF(subq_82.views, 0) AS DOUBLE PRECISION)) AS bookings_per_view -FROM ( +WITH cm_6_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] -- Aggregate Measures @@ -44,8 +39,9 @@ FROM ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_51 -FULL OUTER JOIN ( +) + +, cm_7_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['views', 'listing__capacity_latest'] -- Aggregate Measures @@ -77,87 +73,110 @@ FULL OUTER JOIN ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_61 -ON - subq_51.listing__capacity_latest = subq_61.listing__capacity_latest -FULL OUTER JOIN ( - -- Combine Aggregated Outputs +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_71.bookings) AS bookings - , MAX(subq_81.views) AS views + listing__capacity_latest + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_63.metric_time__day AS metric_time__day + , subq_63.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_63.metric_time__day AS metric_time__day - , subq_63.bookings AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_63 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_63.listing = listings_latest_src_28000.listing_id - ) subq_67 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_71 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['views', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_63 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_63.listing = listings_latest_src_28000.listing_id + ) subq_67 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') + GROUP BY + listing__capacity_latest +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['views', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , SUM(views) AS views + FROM ( + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(views) AS views + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_73.metric_time__day AS metric_time__day + , subq_73.views AS views FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_73.metric_time__day AS metric_time__day - , subq_73.views AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_73 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_73.listing = listings_latest_src_28000.listing_id - ) subq_77 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_81 - ON - subq_71.listing__capacity_latest = subq_81.listing__capacity_latest + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_73 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_73.listing = listings_latest_src_28000.listing_id + ) subq_77 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) -) subq_82 + listing__capacity_latest +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , CAST(bookings AS DOUBLE PRECISION) / CAST(NULLIF(views, 0) AS DOUBLE PRECISION) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_8_cte.listing__capacity_latest, cm_9_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_8_cte.bookings) AS bookings + , MAX(cm_9_cte.views) AS views + FROM cm_8_cte cm_8_cte + FULL OUTER JOIN + cm_9_cte cm_9_cte + ON + cm_8_cte.listing__capacity_latest = cm_9_cte.listing__capacity_latest + GROUP BY + COALESCE(cm_8_cte.listing__capacity_latest, cm_9_cte.listing__capacity_latest) + ) subq_82 +) + +SELECT + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, cm_10_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.views) AS views + , MAX(cm_10_cte.bookings_per_view) AS bookings_per_view +FROM cm_6_cte cm_6_cte +FULL OUTER JOIN + cm_7_cte cm_7_cte +ON + cm_6_cte.listing__capacity_latest = cm_7_cte.listing__capacity_latest +FULL OUTER JOIN + cm_10_cte cm_10_cte ON - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest) = subq_82.listing__capacity_latest + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest) = cm_10_cte.listing__capacity_latest GROUP BY - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, cm_10_cte.listing__capacity_latest) diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql index 1f5468866..e5207ba82 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql @@ -12,52 +12,65 @@ docstring: the source input for the latter input must NOT have the filter applied to it. sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(average_booking_value AS DOUBLE PRECISION) / CAST(NULLIF(max_booking_value, 0) AS DOUBLE PRECISION) AS instant_booking_fraction_of_max_value -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.average_booking_value) AS average_booking_value - , MAX(subq_22.max_booking_value) AS max_booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATE_TRUNC('day', ds) AS metric_time__day - , MAX(booking_value) AS max_booking_value + , is_instant AS booking__is_instant + , booking_value AS average_booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , MAX(booking_value) AS max_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 GROUP BY - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) -) subq_23 + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(average_booking_value AS DOUBLE PRECISION) / CAST(NULLIF(max_booking_value, 0) AS DOUBLE PRECISION) AS instant_booking_fraction_of_max_value + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.average_booking_value) AS average_booking_value + , MAX(cm_7_cte.max_booking_value) AS max_booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_fraction_of_max_value AS instant_booking_fraction_of_max_value +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql index 26fd818dd..2dfeb1449 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql @@ -6,47 +6,97 @@ docstring: TODO: support metric time filters sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest - , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_40.ds AS metric_time__day + , subq_38.listing__country_latest AS listing__country_latest + , subq_38.bookings AS bookings + FROM ***************************.mf_time_spine subq_40 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures SELECT - subq_40.ds AS metric_time__day - , subq_38.listing__country_latest AS listing__country_latest - , subq_38.bookings AS bookings - FROM ***************************.mf_time_spine subq_40 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_31.metric_time__day AS metric_time__day + , subq_31.booking__is_instant AS booking__is_instant + , subq_31.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_31.metric_time__day AS metric_time__day - , subq_31.booking__is_instant AS booking__is_instant - , subq_31.bookings AS bookings - FROM ( + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_31.listing = listings_latest_src_28000.listing_id + ) subq_35 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_38 + ON + subq_40.ds = subq_38.metric_time__day + ) subq_41 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_2_weeks_ago + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_56.ds AS metric_time__day + , subq_54.listing__country_latest AS listing__country_latest + , subq_54.bookings AS bookings + FROM ***************************.mf_time_spine subq_56 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_47.metric_time__day AS metric_time__day + , subq_47.booking__is_instant AS booking__is_instant + , subq_47.bookings AS bookings + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_46.ds AS metric_time__day + , subq_44.listing AS listing + , subq_44.booking__is_instant AS booking__is_instant + , subq_44.bookings AS bookings + FROM ***************************.mf_time_spine subq_46 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -55,91 +105,68 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_44 ON - subq_31.listing = listings_latest_src_28000.listing_id - ) subq_35 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_38 - ON - subq_40.ds = subq_38.metric_time__day - ) subq_41 - ) subq_42 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + DATEADD(day, -14, subq_46.ds) = subq_44.metric_time__day + ) subq_47 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_47.listing = listings_latest_src_28000.listing_id + ) subq_51 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_54 + ON + subq_56.ds = subq_54.metric_time__day + ) subq_57 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_2_weeks_ago + COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day + , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest + , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_6 SELECT - subq_56.ds AS metric_time__day - , subq_54.listing__country_latest AS listing__country_latest - , subq_54.bookings AS bookings - FROM ***************************.mf_time_spine subq_56 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_47.metric_time__day AS metric_time__day - , subq_47.booking__is_instant AS booking__is_instant - , subq_47.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_46.ds AS metric_time__day - , subq_44.listing AS listing - , subq_44.booking__is_instant AS booking__is_instant - , subq_44.bookings AS bookings - FROM ***************************.mf_time_spine subq_46 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_44 - ON - DATEADD(day, -14, subq_46.ds) = subq_44.metric_time__day - ) subq_47 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_47.listing = listings_latest_src_28000.listing_id - ) subq_51 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_54 - ON - subq_56.ds = subq_54.metric_time__day - ) subq_57 - ) subq_58 - ON - ( - subq_42.listing__country_latest = subq_58.listing__country_latest - ) AND ( - subq_42.metric_time__day = subq_58.metric_time__day - ) - GROUP BY - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) -) subq_59 + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_42 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , listing__country_latest + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_58 + ON + ( + subq_42.listing__country_latest = subq_58.listing__country_latest + ) AND ( + subq_42.metric_time__day = subq_58.metric_time__day + ) + GROUP BY + COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) + , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) + ) subq_59 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks_fill_nulls_with_0 AS bookings_growth_2_weeks_fill_nulls_with_0 +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_offset_metric_with_query_time_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_offset_metric_with_query_time_filters__plan0_optimized.sql index 360555b7c..334d61d84 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_offset_metric_with_query_time_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_offset_metric_with_query_time_filters__plan0_optimized.sql @@ -6,35 +6,69 @@ docstring: TODO: support metric time filters sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_25.metric_time__day AS metric_time__day + , subq_25.booking__is_instant AS booking__is_instant + , subq_25.bookings AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_25.listing = listings_latest_src_28000.listing_id + ) subq_29 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) AS metric_time__day - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) AS listing__country_latest - , MAX(subq_33.bookings) AS bookings - , MAX(subq_46.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings_2_weeks_ago FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_38.metric_time__day AS metric_time__day + , subq_38.booking__is_instant AS booking__is_instant + , subq_38.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Join to Time Spine Dataset SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_25.metric_time__day AS metric_time__day - , subq_25.booking__is_instant AS booking__is_instant - , subq_25.bookings AS bookings - FROM ( + subq_37.ds AS metric_time__day + , subq_35.listing AS listing + , subq_35.booking__is_instant AS booking__is_instant + , subq_35.bookings AS bookings + FROM ***************************.mf_time_spine subq_37 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -43,71 +77,51 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_35 ON - subq_25.listing = listings_latest_src_28000.listing_id - ) subq_29 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_33 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATEADD(day, -14, subq_37.ds) = subq_35.metric_time__day + ) subq_38 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_38.listing = listings_latest_src_28000.listing_id + ) subq_42 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_38.metric_time__day AS metric_time__day - , subq_38.booking__is_instant AS booking__is_instant - , subq_38.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_37.ds AS metric_time__day - , subq_35.listing AS listing - , subq_35.booking__is_instant AS booking__is_instant - , subq_35.bookings AS bookings - FROM ***************************.mf_time_spine subq_37 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_35 - ON - DATEADD(day, -14, subq_37.ds) = subq_35.metric_time__day - ) subq_38 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_38.listing = listings_latest_src_28000.listing_id - ) subq_42 - WHERE booking__is_instant + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.listing__country_latest, cm_7_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + ( + cm_6_cte.listing__country_latest = cm_7_cte.listing__country_latest + ) AND ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) GROUP BY - metric_time__day - , listing__country_latest - ) subq_46 - ON - ( - subq_33.listing__country_latest = subq_46.listing__country_latest - ) AND ( - subq_33.metric_time__day = subq_46.metric_time__day - ) - GROUP BY - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) -) subq_47 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + , COALESCE(cm_6_cte.listing__country_latest, cm_7_cte.listing__country_latest) + ) subq_47 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql index 907567ef0..5d31af894 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql @@ -7,12 +7,7 @@ docstring: sql_engine: Redshift --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_51.bookings) AS bookings - , MAX(subq_61.views) AS views - , MAX(CAST(subq_82.bookings AS DOUBLE PRECISION) / CAST(NULLIF(subq_82.views, 0) AS DOUBLE PRECISION)) AS bookings_per_view -FROM ( +WITH cm_6_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] -- Aggregate Measures @@ -44,8 +39,9 @@ FROM ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_51 -FULL OUTER JOIN ( +) + +, cm_7_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['views', 'listing__capacity_latest'] -- Aggregate Measures @@ -77,87 +73,110 @@ FULL OUTER JOIN ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_61 -ON - subq_51.listing__capacity_latest = subq_61.listing__capacity_latest -FULL OUTER JOIN ( - -- Combine Aggregated Outputs +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_71.bookings) AS bookings - , MAX(subq_81.views) AS views + listing__capacity_latest + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_63.metric_time__day AS metric_time__day + , subq_63.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_63.metric_time__day AS metric_time__day - , subq_63.bookings AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_63 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_63.listing = listings_latest_src_28000.listing_id - ) subq_67 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_71 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['views', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_63 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_63.listing = listings_latest_src_28000.listing_id + ) subq_67 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') + GROUP BY + listing__capacity_latest +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['views', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , SUM(views) AS views + FROM ( + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(views) AS views + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_73.metric_time__day AS metric_time__day + , subq_73.views AS views FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_73.metric_time__day AS metric_time__day - , subq_73.views AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_73 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_73.listing = listings_latest_src_28000.listing_id - ) subq_77 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_81 - ON - subq_71.listing__capacity_latest = subq_81.listing__capacity_latest + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_73 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_73.listing = listings_latest_src_28000.listing_id + ) subq_77 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) -) subq_82 + listing__capacity_latest +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , CAST(bookings AS DOUBLE PRECISION) / CAST(NULLIF(views, 0) AS DOUBLE PRECISION) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_8_cte.listing__capacity_latest, cm_9_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_8_cte.bookings) AS bookings + , MAX(cm_9_cte.views) AS views + FROM cm_8_cte cm_8_cte + FULL OUTER JOIN + cm_9_cte cm_9_cte + ON + cm_8_cte.listing__capacity_latest = cm_9_cte.listing__capacity_latest + GROUP BY + COALESCE(cm_8_cte.listing__capacity_latest, cm_9_cte.listing__capacity_latest) + ) subq_82 +) + +SELECT + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, cm_10_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.views) AS views + , MAX(cm_10_cte.bookings_per_view) AS bookings_per_view +FROM cm_6_cte cm_6_cte +FULL OUTER JOIN + cm_7_cte cm_7_cte +ON + cm_6_cte.listing__capacity_latest = cm_7_cte.listing__capacity_latest +FULL OUTER JOIN + cm_10_cte cm_10_cte ON - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest) = subq_82.listing__capacity_latest + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest) = cm_10_cte.listing__capacity_latest GROUP BY - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, cm_10_cte.listing__capacity_latest) diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql index 3a8e14329..1649272e0 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql @@ -12,52 +12,65 @@ docstring: the source input for the latter input must NOT have the filter applied to it. sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(average_booking_value AS DOUBLE) / CAST(NULLIF(max_booking_value, 0) AS DOUBLE) AS instant_booking_fraction_of_max_value -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.average_booking_value) AS average_booking_value - , MAX(subq_22.max_booking_value) AS max_booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATE_TRUNC('day', ds) AS metric_time__day - , MAX(booking_value) AS max_booking_value + , is_instant AS booking__is_instant + , booking_value AS average_booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , MAX(booking_value) AS max_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 GROUP BY - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) -) subq_23 + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(average_booking_value AS DOUBLE) / CAST(NULLIF(max_booking_value, 0) AS DOUBLE) AS instant_booking_fraction_of_max_value + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.average_booking_value) AS average_booking_value + , MAX(cm_7_cte.max_booking_value) AS max_booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_fraction_of_max_value AS instant_booking_fraction_of_max_value +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql index 0910734c3..5add6d3c9 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql @@ -6,47 +6,97 @@ docstring: TODO: support metric time filters sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest - , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_40.ds AS metric_time__day + , subq_38.listing__country_latest AS listing__country_latest + , subq_38.bookings AS bookings + FROM ***************************.mf_time_spine subq_40 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures SELECT - subq_40.ds AS metric_time__day - , subq_38.listing__country_latest AS listing__country_latest - , subq_38.bookings AS bookings - FROM ***************************.mf_time_spine subq_40 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_31.metric_time__day AS metric_time__day + , subq_31.booking__is_instant AS booking__is_instant + , subq_31.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_31.metric_time__day AS metric_time__day - , subq_31.booking__is_instant AS booking__is_instant - , subq_31.bookings AS bookings - FROM ( + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_31.listing = listings_latest_src_28000.listing_id + ) subq_35 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_38 + ON + subq_40.ds = subq_38.metric_time__day + ) subq_41 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_2_weeks_ago + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_56.ds AS metric_time__day + , subq_54.listing__country_latest AS listing__country_latest + , subq_54.bookings AS bookings + FROM ***************************.mf_time_spine subq_56 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_47.metric_time__day AS metric_time__day + , subq_47.booking__is_instant AS booking__is_instant + , subq_47.bookings AS bookings + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_46.ds AS metric_time__day + , subq_44.listing AS listing + , subq_44.booking__is_instant AS booking__is_instant + , subq_44.bookings AS bookings + FROM ***************************.mf_time_spine subq_46 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -55,91 +105,68 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_44 ON - subq_31.listing = listings_latest_src_28000.listing_id - ) subq_35 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_38 - ON - subq_40.ds = subq_38.metric_time__day - ) subq_41 - ) subq_42 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + DATEADD(day, -14, subq_46.ds) = subq_44.metric_time__day + ) subq_47 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_47.listing = listings_latest_src_28000.listing_id + ) subq_51 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_54 + ON + subq_56.ds = subq_54.metric_time__day + ) subq_57 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_2_weeks_ago + COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day + , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest + , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_6 SELECT - subq_56.ds AS metric_time__day - , subq_54.listing__country_latest AS listing__country_latest - , subq_54.bookings AS bookings - FROM ***************************.mf_time_spine subq_56 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_47.metric_time__day AS metric_time__day - , subq_47.booking__is_instant AS booking__is_instant - , subq_47.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_46.ds AS metric_time__day - , subq_44.listing AS listing - , subq_44.booking__is_instant AS booking__is_instant - , subq_44.bookings AS bookings - FROM ***************************.mf_time_spine subq_46 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_44 - ON - DATEADD(day, -14, subq_46.ds) = subq_44.metric_time__day - ) subq_47 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_47.listing = listings_latest_src_28000.listing_id - ) subq_51 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_54 - ON - subq_56.ds = subq_54.metric_time__day - ) subq_57 - ) subq_58 - ON - ( - subq_42.listing__country_latest = subq_58.listing__country_latest - ) AND ( - subq_42.metric_time__day = subq_58.metric_time__day - ) - GROUP BY - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) -) subq_59 + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_42 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , listing__country_latest + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_58 + ON + ( + subq_42.listing__country_latest = subq_58.listing__country_latest + ) AND ( + subq_42.metric_time__day = subq_58.metric_time__day + ) + GROUP BY + COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) + , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) + ) subq_59 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks_fill_nulls_with_0 AS bookings_growth_2_weeks_fill_nulls_with_0 +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_offset_metric_with_query_time_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_offset_metric_with_query_time_filters__plan0_optimized.sql index 0fbcd96ac..bfcf06a48 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_offset_metric_with_query_time_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_offset_metric_with_query_time_filters__plan0_optimized.sql @@ -6,35 +6,69 @@ docstring: TODO: support metric time filters sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_25.metric_time__day AS metric_time__day + , subq_25.booking__is_instant AS booking__is_instant + , subq_25.bookings AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_25.listing = listings_latest_src_28000.listing_id + ) subq_29 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) AS metric_time__day - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) AS listing__country_latest - , MAX(subq_33.bookings) AS bookings - , MAX(subq_46.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings_2_weeks_ago FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_38.metric_time__day AS metric_time__day + , subq_38.booking__is_instant AS booking__is_instant + , subq_38.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Join to Time Spine Dataset SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_25.metric_time__day AS metric_time__day - , subq_25.booking__is_instant AS booking__is_instant - , subq_25.bookings AS bookings - FROM ( + subq_37.ds AS metric_time__day + , subq_35.listing AS listing + , subq_35.booking__is_instant AS booking__is_instant + , subq_35.bookings AS bookings + FROM ***************************.mf_time_spine subq_37 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -43,71 +77,51 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_35 ON - subq_25.listing = listings_latest_src_28000.listing_id - ) subq_29 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_33 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATEADD(day, -14, subq_37.ds) = subq_35.metric_time__day + ) subq_38 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_38.listing = listings_latest_src_28000.listing_id + ) subq_42 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_38.metric_time__day AS metric_time__day - , subq_38.booking__is_instant AS booking__is_instant - , subq_38.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_37.ds AS metric_time__day - , subq_35.listing AS listing - , subq_35.booking__is_instant AS booking__is_instant - , subq_35.bookings AS bookings - FROM ***************************.mf_time_spine subq_37 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_35 - ON - DATEADD(day, -14, subq_37.ds) = subq_35.metric_time__day - ) subq_38 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_38.listing = listings_latest_src_28000.listing_id - ) subq_42 - WHERE booking__is_instant + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.listing__country_latest, cm_7_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + ( + cm_6_cte.listing__country_latest = cm_7_cte.listing__country_latest + ) AND ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) GROUP BY - metric_time__day - , listing__country_latest - ) subq_46 - ON - ( - subq_33.listing__country_latest = subq_46.listing__country_latest - ) AND ( - subq_33.metric_time__day = subq_46.metric_time__day - ) - GROUP BY - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) -) subq_47 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + , COALESCE(cm_6_cte.listing__country_latest, cm_7_cte.listing__country_latest) + ) subq_47 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql index 4076772c0..b2cc1d5d4 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql @@ -7,12 +7,7 @@ docstring: sql_engine: Snowflake --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_51.bookings) AS bookings - , MAX(subq_61.views) AS views - , MAX(CAST(subq_82.bookings AS DOUBLE) / CAST(NULLIF(subq_82.views, 0) AS DOUBLE)) AS bookings_per_view -FROM ( +WITH cm_6_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] -- Aggregate Measures @@ -44,8 +39,9 @@ FROM ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_51 -FULL OUTER JOIN ( +) + +, cm_7_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['views', 'listing__capacity_latest'] -- Aggregate Measures @@ -77,87 +73,110 @@ FULL OUTER JOIN ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_61 -ON - subq_51.listing__capacity_latest = subq_61.listing__capacity_latest -FULL OUTER JOIN ( - -- Combine Aggregated Outputs +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_71.bookings) AS bookings - , MAX(subq_81.views) AS views + listing__capacity_latest + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_63.metric_time__day AS metric_time__day + , subq_63.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_63.metric_time__day AS metric_time__day - , subq_63.bookings AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_63 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_63.listing = listings_latest_src_28000.listing_id - ) subq_67 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_71 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['views', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_63 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_63.listing = listings_latest_src_28000.listing_id + ) subq_67 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') + GROUP BY + listing__capacity_latest +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['views', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , SUM(views) AS views + FROM ( + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(views) AS views + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_73.metric_time__day AS metric_time__day + , subq_73.views AS views FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_73.metric_time__day AS metric_time__day - , subq_73.views AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_73 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_73.listing = listings_latest_src_28000.listing_id - ) subq_77 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_81 - ON - subq_71.listing__capacity_latest = subq_81.listing__capacity_latest + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_73 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_73.listing = listings_latest_src_28000.listing_id + ) subq_77 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) -) subq_82 + listing__capacity_latest +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_8_cte.listing__capacity_latest, cm_9_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_8_cte.bookings) AS bookings + , MAX(cm_9_cte.views) AS views + FROM cm_8_cte cm_8_cte + FULL OUTER JOIN + cm_9_cte cm_9_cte + ON + cm_8_cte.listing__capacity_latest = cm_9_cte.listing__capacity_latest + GROUP BY + COALESCE(cm_8_cte.listing__capacity_latest, cm_9_cte.listing__capacity_latest) + ) subq_82 +) + +SELECT + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, cm_10_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.views) AS views + , MAX(cm_10_cte.bookings_per_view) AS bookings_per_view +FROM cm_6_cte cm_6_cte +FULL OUTER JOIN + cm_7_cte cm_7_cte +ON + cm_6_cte.listing__capacity_latest = cm_7_cte.listing__capacity_latest +FULL OUTER JOIN + cm_10_cte cm_10_cte ON - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest) = subq_82.listing__capacity_latest + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest) = cm_10_cte.listing__capacity_latest GROUP BY - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, cm_10_cte.listing__capacity_latest) diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql index 88489a386..59e7c04db 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql @@ -12,52 +12,65 @@ docstring: the source input for the latter input must NOT have the filter applied to it. sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(average_booking_value AS DOUBLE) / CAST(NULLIF(max_booking_value, 0) AS DOUBLE) AS instant_booking_fraction_of_max_value -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.average_booking_value) AS average_booking_value - , MAX(subq_22.max_booking_value) AS max_booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATE_TRUNC('day', ds) AS metric_time__day - , MAX(booking_value) AS max_booking_value + , is_instant AS booking__is_instant + , booking_value AS average_booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , MAX(booking_value) AS max_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 GROUP BY - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) -) subq_23 + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(average_booking_value AS DOUBLE) / CAST(NULLIF(max_booking_value, 0) AS DOUBLE) AS instant_booking_fraction_of_max_value + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.average_booking_value) AS average_booking_value + , MAX(cm_7_cte.max_booking_value) AS max_booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_fraction_of_max_value AS instant_booking_fraction_of_max_value +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql index 0a8042dc5..893e58633 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql @@ -6,47 +6,97 @@ docstring: TODO: support metric time filters sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest - , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_40.ds AS metric_time__day + , subq_38.listing__country_latest AS listing__country_latest + , subq_38.bookings AS bookings + FROM ***************************.mf_time_spine subq_40 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures SELECT - subq_40.ds AS metric_time__day - , subq_38.listing__country_latest AS listing__country_latest - , subq_38.bookings AS bookings - FROM ***************************.mf_time_spine subq_40 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_31.metric_time__day AS metric_time__day + , subq_31.booking__is_instant AS booking__is_instant + , subq_31.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_31.metric_time__day AS metric_time__day - , subq_31.booking__is_instant AS booking__is_instant - , subq_31.bookings AS bookings - FROM ( + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_31.listing = listings_latest_src_28000.listing_id + ) subq_35 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_38 + ON + subq_40.ds = subq_38.metric_time__day + ) subq_41 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_2_weeks_ago + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_56.ds AS metric_time__day + , subq_54.listing__country_latest AS listing__country_latest + , subq_54.bookings AS bookings + FROM ***************************.mf_time_spine subq_56 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_47.metric_time__day AS metric_time__day + , subq_47.booking__is_instant AS booking__is_instant + , subq_47.bookings AS bookings + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_46.ds AS metric_time__day + , subq_44.listing AS listing + , subq_44.booking__is_instant AS booking__is_instant + , subq_44.bookings AS bookings + FROM ***************************.mf_time_spine subq_46 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -55,91 +105,68 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_44 ON - subq_31.listing = listings_latest_src_28000.listing_id - ) subq_35 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_38 - ON - subq_40.ds = subq_38.metric_time__day - ) subq_41 - ) subq_42 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + DATE_ADD('day', -14, subq_46.ds) = subq_44.metric_time__day + ) subq_47 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_47.listing = listings_latest_src_28000.listing_id + ) subq_51 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_54 + ON + subq_56.ds = subq_54.metric_time__day + ) subq_57 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_2_weeks_ago + COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day + , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest + , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_6 SELECT - subq_56.ds AS metric_time__day - , subq_54.listing__country_latest AS listing__country_latest - , subq_54.bookings AS bookings - FROM ***************************.mf_time_spine subq_56 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_47.metric_time__day AS metric_time__day - , subq_47.booking__is_instant AS booking__is_instant - , subq_47.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_46.ds AS metric_time__day - , subq_44.listing AS listing - , subq_44.booking__is_instant AS booking__is_instant - , subq_44.bookings AS bookings - FROM ***************************.mf_time_spine subq_46 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_44 - ON - DATE_ADD('day', -14, subq_46.ds) = subq_44.metric_time__day - ) subq_47 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_47.listing = listings_latest_src_28000.listing_id - ) subq_51 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_54 - ON - subq_56.ds = subq_54.metric_time__day - ) subq_57 - ) subq_58 - ON - ( - subq_42.listing__country_latest = subq_58.listing__country_latest - ) AND ( - subq_42.metric_time__day = subq_58.metric_time__day - ) - GROUP BY - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) -) subq_59 + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_42 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , listing__country_latest + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_58 + ON + ( + subq_42.listing__country_latest = subq_58.listing__country_latest + ) AND ( + subq_42.metric_time__day = subq_58.metric_time__day + ) + GROUP BY + COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) + , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) + ) subq_59 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks_fill_nulls_with_0 AS bookings_growth_2_weeks_fill_nulls_with_0 +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_offset_metric_with_query_time_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_offset_metric_with_query_time_filters__plan0_optimized.sql index 2187c0708..bc7a2e5f4 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_offset_metric_with_query_time_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_offset_metric_with_query_time_filters__plan0_optimized.sql @@ -6,35 +6,69 @@ docstring: TODO: support metric time filters sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_25.metric_time__day AS metric_time__day + , subq_25.booking__is_instant AS booking__is_instant + , subq_25.bookings AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_25.listing = listings_latest_src_28000.listing_id + ) subq_29 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) AS metric_time__day - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) AS listing__country_latest - , MAX(subq_33.bookings) AS bookings - , MAX(subq_46.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings_2_weeks_ago FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_38.metric_time__day AS metric_time__day + , subq_38.booking__is_instant AS booking__is_instant + , subq_38.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Join to Time Spine Dataset SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_25.metric_time__day AS metric_time__day - , subq_25.booking__is_instant AS booking__is_instant - , subq_25.bookings AS bookings - FROM ( + subq_37.ds AS metric_time__day + , subq_35.listing AS listing + , subq_35.booking__is_instant AS booking__is_instant + , subq_35.bookings AS bookings + FROM ***************************.mf_time_spine subq_37 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -43,71 +77,51 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_35 ON - subq_25.listing = listings_latest_src_28000.listing_id - ) subq_29 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_33 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_ADD('day', -14, subq_37.ds) = subq_35.metric_time__day + ) subq_38 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_38.listing = listings_latest_src_28000.listing_id + ) subq_42 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_38.metric_time__day AS metric_time__day - , subq_38.booking__is_instant AS booking__is_instant - , subq_38.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_37.ds AS metric_time__day - , subq_35.listing AS listing - , subq_35.booking__is_instant AS booking__is_instant - , subq_35.bookings AS bookings - FROM ***************************.mf_time_spine subq_37 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_35 - ON - DATE_ADD('day', -14, subq_37.ds) = subq_35.metric_time__day - ) subq_38 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_38.listing = listings_latest_src_28000.listing_id - ) subq_42 - WHERE booking__is_instant + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.listing__country_latest, cm_7_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + ( + cm_6_cte.listing__country_latest = cm_7_cte.listing__country_latest + ) AND ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) GROUP BY - metric_time__day - , listing__country_latest - ) subq_46 - ON - ( - subq_33.listing__country_latest = subq_46.listing__country_latest - ) AND ( - subq_33.metric_time__day = subq_46.metric_time__day - ) - GROUP BY - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) -) subq_47 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + , COALESCE(cm_6_cte.listing__country_latest, cm_7_cte.listing__country_latest) + ) subq_47 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql index 91f21bfa1..ae24f5d9b 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql @@ -7,12 +7,7 @@ docstring: sql_engine: Trino --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_51.bookings) AS bookings - , MAX(subq_61.views) AS views - , MAX(CAST(subq_82.bookings AS DOUBLE) / CAST(NULLIF(subq_82.views, 0) AS DOUBLE)) AS bookings_per_view -FROM ( +WITH cm_6_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] -- Aggregate Measures @@ -44,8 +39,9 @@ FROM ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_51 -FULL OUTER JOIN ( +) + +, cm_7_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['views', 'listing__capacity_latest'] -- Aggregate Measures @@ -77,87 +73,110 @@ FULL OUTER JOIN ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_61 -ON - subq_51.listing__capacity_latest = subq_61.listing__capacity_latest -FULL OUTER JOIN ( - -- Combine Aggregated Outputs +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_71.bookings) AS bookings - , MAX(subq_81.views) AS views + listing__capacity_latest + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_63.metric_time__day AS metric_time__day + , subq_63.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_63.metric_time__day AS metric_time__day - , subq_63.bookings AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_63 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_63.listing = listings_latest_src_28000.listing_id - ) subq_67 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_71 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['views', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_63 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_63.listing = listings_latest_src_28000.listing_id + ) subq_67 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') + GROUP BY + listing__capacity_latest +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['views', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , SUM(views) AS views + FROM ( + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(views) AS views + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_73.metric_time__day AS metric_time__day + , subq_73.views AS views FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_73.metric_time__day AS metric_time__day - , subq_73.views AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_73 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_73.listing = listings_latest_src_28000.listing_id - ) subq_77 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_81 - ON - subq_71.listing__capacity_latest = subq_81.listing__capacity_latest + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_73 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_73.listing = listings_latest_src_28000.listing_id + ) subq_77 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) -) subq_82 + listing__capacity_latest +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_8_cte.listing__capacity_latest, cm_9_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_8_cte.bookings) AS bookings + , MAX(cm_9_cte.views) AS views + FROM cm_8_cte cm_8_cte + FULL OUTER JOIN + cm_9_cte cm_9_cte + ON + cm_8_cte.listing__capacity_latest = cm_9_cte.listing__capacity_latest + GROUP BY + COALESCE(cm_8_cte.listing__capacity_latest, cm_9_cte.listing__capacity_latest) + ) subq_82 +) + +SELECT + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, cm_10_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.views) AS views + , MAX(cm_10_cte.bookings_per_view) AS bookings_per_view +FROM cm_6_cte cm_6_cte +FULL OUTER JOIN + cm_7_cte cm_7_cte +ON + cm_6_cte.listing__capacity_latest = cm_7_cte.listing__capacity_latest +FULL OUTER JOIN + cm_10_cte cm_10_cte ON - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest) = subq_82.listing__capacity_latest + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest) = cm_10_cte.listing__capacity_latest GROUP BY - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, cm_10_cte.listing__capacity_latest) diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_measure_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_measure_constraint__plan0_optimized.sql index 407adbf90..affb382b1 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_measure_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_measure_constraint__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_measure_constraint test_filename: test_query_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_12 +WITH cm_11_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_35.metric_time__day, subq_40.metric_time__day) AS metric_time__day - , MAX(subq_35.average_booking_value) AS average_booking_value - , MAX(subq_35.bookings) AS bookings - , MAX(subq_40.booking_value) AS booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_27.metric_time__day AS metric_time__day + , subq_27.bookings AS bookings + , subq_27.average_booking_value AS average_booking_value FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_27.metric_time__day AS metric_time__day - , subq_27.bookings AS bookings - , subq_27.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_27 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_27.listing = listings_latest_src_28000.listing_id - ) subq_31 - WHERE listing__is_lux_latest - GROUP BY - metric_time__day - ) subq_35 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATETIME_TRUNC(ds, day) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_27 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_27.listing = listings_latest_src_28000.listing_id + ) subq_31 + WHERE listing__is_lux_latest + GROUP BY + metric_time__day +) + +, cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + metric_time__day +) + +, cm_12_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr + FROM ( + -- Combine Aggregated Outputs SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_11_cte.metric_time__day, cm_10_cte.metric_time__day) AS metric_time__day + , MAX(cm_11_cte.average_booking_value) AS average_booking_value + , MAX(cm_11_cte.bookings) AS bookings + , MAX(cm_10_cte.booking_value) AS booking_value + FROM cm_11_cte cm_11_cte + FULL OUTER JOIN + cm_10_cte cm_10_cte + ON + cm_11_cte.metric_time__day = cm_10_cte.metric_time__day GROUP BY metric_time__day - ) subq_40 - ON - subq_35.metric_time__day = subq_40.metric_time__day - GROUP BY - metric_time__day -) subq_41 + ) subq_41 +) + +SELECT + metric_time__day AS metric_time__day + , lux_booking_value_rate_expr AS lux_booking_value_rate_expr +FROM cm_12_cte cm_12_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_measure_constraint_with_reused_measure__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_measure_constraint_with_reused_measure__plan0_optimized.sql index df236298a..c0649bd6a 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_measure_constraint_with_reused_measure__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_measure_constraint_with_reused_measure__plan0_optimized.sql @@ -2,52 +2,65 @@ test_name: test_measure_constraint_with_reused_measure test_filename: test_query_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(booking_value_with_is_instant_constraint AS FLOAT64) / CAST(NULLIF(booking_value, 0) AS FLOAT64) AS instant_booking_value_ratio -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint - , MAX(subq_22.booking_value) AS booking_value + metric_time__day + , SUM(booking_value) AS booking_value_with_is_instant_constraint FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(booking_value) AS booking_value_with_is_instant_constraint - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATETIME_TRUNC(ds, day) AS metric_time__day - , SUM(booking_value) AS booking_value + , is_instant AS booking__is_instant + , booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - metric_time__day - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 GROUP BY metric_time__day -) subq_23 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(booking_value_with_is_instant_constraint AS FLOAT64) / CAST(NULLIF(booking_value, 0) AS FLOAT64) AS instant_booking_value_ratio + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint + , MAX(cm_7_cte.booking_value) AS booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_value_ratio AS instant_booking_value_ratio +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql index 1d74c8190..5efd2ee15 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_measure_constraint_with_single_expr_and_alias test_filename: test_query_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , delayed_bookings * 2 AS double_counted_delayed_bookings -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -26,4 +23,23 @@ FROM ( WHERE NOT booking__is_instant GROUP BY metric_time__day -) subq_11 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , delayed_bookings * 2 AS double_counted_delayed_bookings + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , delayed_bookings + FROM cm_4_cte cm_4_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , double_counted_delayed_bookings AS double_counted_delayed_bookings +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql index 62a40bc24..0ba7b73d4 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_metric_with_measures_from_multiple_sources_no_dimensions test_filename: test_query_rendering.py sql_engine: BigQuery --- --- Combine Aggregated Outputs --- Compute Metrics via Expressions -SELECT - CAST(MAX(subq_15.bookings) AS FLOAT64) / CAST(NULLIF(MAX(subq_20.listings), 0) AS FLOAT64) AS bookings_per_listing -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings',] @@ -15,8 +12,9 @@ FROM ( SELECT SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 -) subq_15 -CROSS JOIN ( +) + +, cm_7_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['listings',] @@ -25,4 +23,18 @@ CROSS JOIN ( SELECT SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 -) subq_20 +) + +, cm_8_cte AS ( + -- Combine Aggregated Outputs + -- Compute Metrics via Expressions + SELECT + CAST(MAX(cm_6_cte.bookings) AS FLOAT64) / CAST(NULLIF(MAX(cm_7_cte.listings), 0) AS FLOAT64) AS bookings_per_listing + FROM cm_6_cte cm_6_cte + CROSS JOIN + cm_7_cte cm_7_cte +) + +SELECT + bookings_per_listing AS bookings_per_listing +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_multiple_metrics_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_multiple_metrics_no_dimensions__plan0_optimized.sql index e273bc2e0..93b068558 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_multiple_metrics_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_multiple_metrics_no_dimensions__plan0_optimized.sql @@ -3,10 +3,7 @@ test_filename: test_query_rendering.py sql_engine: BigQuery --- -- Combine Aggregated Outputs -SELECT - MAX(subq_17.bookings) AS bookings - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_4_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -17,8 +14,9 @@ FROM ( SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 WHERE DATETIME_TRUNC(ds, day) BETWEEN '2020-01-01' AND '2020-01-01' -) subq_17 -CROSS JOIN ( +) + +, cm_5_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -29,4 +27,11 @@ CROSS JOIN ( SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 WHERE DATETIME_TRUNC(created_at, day) BETWEEN '2020-01-01' AND '2020-01-01' -) subq_23 +) + +SELECT + MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +CROSS JOIN + cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_measure_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_measure_constraint__plan0_optimized.sql index 32b76b94d..c41c0dc2c 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_measure_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_measure_constraint__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_measure_constraint test_filename: test_query_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_12 +WITH cm_11_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_35.metric_time__day, subq_40.metric_time__day) AS metric_time__day - , MAX(subq_35.average_booking_value) AS average_booking_value - , MAX(subq_35.bookings) AS bookings - , MAX(subq_40.booking_value) AS booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_27.metric_time__day AS metric_time__day + , subq_27.bookings AS bookings + , subq_27.average_booking_value AS average_booking_value FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_27.metric_time__day AS metric_time__day - , subq_27.bookings AS bookings - , subq_27.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_27 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_27.listing = listings_latest_src_28000.listing_id - ) subq_31 - WHERE listing__is_lux_latest - GROUP BY - metric_time__day - ) subq_35 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_27 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_27.listing = listings_latest_src_28000.listing_id + ) subq_31 + WHERE listing__is_lux_latest + GROUP BY + metric_time__day +) + +, cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_12_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr + FROM ( + -- Combine Aggregated Outputs SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_11_cte.metric_time__day, cm_10_cte.metric_time__day) AS metric_time__day + , MAX(cm_11_cte.average_booking_value) AS average_booking_value + , MAX(cm_11_cte.bookings) AS bookings + , MAX(cm_10_cte.booking_value) AS booking_value + FROM cm_11_cte cm_11_cte + FULL OUTER JOIN + cm_10_cte cm_10_cte + ON + cm_11_cte.metric_time__day = cm_10_cte.metric_time__day GROUP BY - DATE_TRUNC('day', ds) - ) subq_40 - ON - subq_35.metric_time__day = subq_40.metric_time__day - GROUP BY - COALESCE(subq_35.metric_time__day, subq_40.metric_time__day) -) subq_41 + COALESCE(cm_11_cte.metric_time__day, cm_10_cte.metric_time__day) + ) subq_41 +) + +SELECT + metric_time__day AS metric_time__day + , lux_booking_value_rate_expr AS lux_booking_value_rate_expr +FROM cm_12_cte cm_12_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_measure_constraint_with_reused_measure__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_measure_constraint_with_reused_measure__plan0_optimized.sql index 0fb229c3c..66f52b7e4 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_measure_constraint_with_reused_measure__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_measure_constraint_with_reused_measure__plan0_optimized.sql @@ -2,52 +2,65 @@ test_name: test_measure_constraint_with_reused_measure test_filename: test_query_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(booking_value_with_is_instant_constraint AS DOUBLE) / CAST(NULLIF(booking_value, 0) AS DOUBLE) AS instant_booking_value_ratio -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint - , MAX(subq_22.booking_value) AS booking_value + metric_time__day + , SUM(booking_value) AS booking_value_with_is_instant_constraint FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(booking_value) AS booking_value_with_is_instant_constraint - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value + , is_instant AS booking__is_instant + , booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 GROUP BY - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) -) subq_23 + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(booking_value_with_is_instant_constraint AS DOUBLE) / CAST(NULLIF(booking_value, 0) AS DOUBLE) AS instant_booking_value_ratio + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint + , MAX(cm_7_cte.booking_value) AS booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_value_ratio AS instant_booking_value_ratio +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql index 5be1cd7cc..07e76472f 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_measure_constraint_with_single_expr_and_alias test_filename: test_query_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , delayed_bookings * 2 AS double_counted_delayed_bookings -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -26,4 +23,23 @@ FROM ( WHERE NOT booking__is_instant GROUP BY metric_time__day -) subq_11 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , delayed_bookings * 2 AS double_counted_delayed_bookings + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , delayed_bookings + FROM cm_4_cte cm_4_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , double_counted_delayed_bookings AS double_counted_delayed_bookings +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql index 35ffb7205..4b5f0de2a 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_metric_with_measures_from_multiple_sources_no_dimensions test_filename: test_query_rendering.py sql_engine: Databricks --- --- Combine Aggregated Outputs --- Compute Metrics via Expressions -SELECT - CAST(MAX(subq_15.bookings) AS DOUBLE) / CAST(NULLIF(MAX(subq_20.listings), 0) AS DOUBLE) AS bookings_per_listing -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings',] @@ -15,8 +12,9 @@ FROM ( SELECT SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 -) subq_15 -CROSS JOIN ( +) + +, cm_7_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['listings',] @@ -25,4 +23,18 @@ CROSS JOIN ( SELECT SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 -) subq_20 +) + +, cm_8_cte AS ( + -- Combine Aggregated Outputs + -- Compute Metrics via Expressions + SELECT + CAST(MAX(cm_6_cte.bookings) AS DOUBLE) / CAST(NULLIF(MAX(cm_7_cte.listings), 0) AS DOUBLE) AS bookings_per_listing + FROM cm_6_cte cm_6_cte + CROSS JOIN + cm_7_cte cm_7_cte +) + +SELECT + bookings_per_listing AS bookings_per_listing +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_multiple_metrics_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_multiple_metrics_no_dimensions__plan0_optimized.sql index 43de92366..0942923b6 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_multiple_metrics_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_multiple_metrics_no_dimensions__plan0_optimized.sql @@ -3,10 +3,7 @@ test_filename: test_query_rendering.py sql_engine: Databricks --- -- Combine Aggregated Outputs -SELECT - MAX(subq_17.bookings) AS bookings - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_4_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -17,8 +14,9 @@ FROM ( SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-01' -) subq_17 -CROSS JOIN ( +) + +, cm_5_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -29,4 +27,11 @@ CROSS JOIN ( SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 WHERE DATE_TRUNC('day', created_at) BETWEEN '2020-01-01' AND '2020-01-01' -) subq_23 +) + +SELECT + MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +CROSS JOIN + cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_measure_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_measure_constraint__plan0_optimized.sql index 4479ea1c9..bee53c6bf 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_measure_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_measure_constraint__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_measure_constraint test_filename: test_query_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_12 +WITH cm_11_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_35.metric_time__day, subq_40.metric_time__day) AS metric_time__day - , MAX(subq_35.average_booking_value) AS average_booking_value - , MAX(subq_35.bookings) AS bookings - , MAX(subq_40.booking_value) AS booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_27.metric_time__day AS metric_time__day + , subq_27.bookings AS bookings + , subq_27.average_booking_value AS average_booking_value FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_27.metric_time__day AS metric_time__day - , subq_27.bookings AS bookings - , subq_27.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_27 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_27.listing = listings_latest_src_28000.listing_id - ) subq_31 - WHERE listing__is_lux_latest - GROUP BY - metric_time__day - ) subq_35 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_27 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_27.listing = listings_latest_src_28000.listing_id + ) subq_31 + WHERE listing__is_lux_latest + GROUP BY + metric_time__day +) + +, cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_12_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr + FROM ( + -- Combine Aggregated Outputs SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_11_cte.metric_time__day, cm_10_cte.metric_time__day) AS metric_time__day + , MAX(cm_11_cte.average_booking_value) AS average_booking_value + , MAX(cm_11_cte.bookings) AS bookings + , MAX(cm_10_cte.booking_value) AS booking_value + FROM cm_11_cte cm_11_cte + FULL OUTER JOIN + cm_10_cte cm_10_cte + ON + cm_11_cte.metric_time__day = cm_10_cte.metric_time__day GROUP BY - DATE_TRUNC('day', ds) - ) subq_40 - ON - subq_35.metric_time__day = subq_40.metric_time__day - GROUP BY - COALESCE(subq_35.metric_time__day, subq_40.metric_time__day) -) subq_41 + COALESCE(cm_11_cte.metric_time__day, cm_10_cte.metric_time__day) + ) subq_41 +) + +SELECT + metric_time__day AS metric_time__day + , lux_booking_value_rate_expr AS lux_booking_value_rate_expr +FROM cm_12_cte cm_12_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_measure_constraint_with_reused_measure__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_measure_constraint_with_reused_measure__plan0_optimized.sql index e17024039..a7ba8abaf 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_measure_constraint_with_reused_measure__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_measure_constraint_with_reused_measure__plan0_optimized.sql @@ -2,52 +2,65 @@ test_name: test_measure_constraint_with_reused_measure test_filename: test_query_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(booking_value_with_is_instant_constraint AS DOUBLE PRECISION) / CAST(NULLIF(booking_value, 0) AS DOUBLE PRECISION) AS instant_booking_value_ratio -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint - , MAX(subq_22.booking_value) AS booking_value + metric_time__day + , SUM(booking_value) AS booking_value_with_is_instant_constraint FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(booking_value) AS booking_value_with_is_instant_constraint - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value + , is_instant AS booking__is_instant + , booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 GROUP BY - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) -) subq_23 + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(booking_value_with_is_instant_constraint AS DOUBLE PRECISION) / CAST(NULLIF(booking_value, 0) AS DOUBLE PRECISION) AS instant_booking_value_ratio + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint + , MAX(cm_7_cte.booking_value) AS booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_value_ratio AS instant_booking_value_ratio +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql index dd13a6123..01892c5d6 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_measure_constraint_with_single_expr_and_alias test_filename: test_query_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , delayed_bookings * 2 AS double_counted_delayed_bookings -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -26,4 +23,23 @@ FROM ( WHERE NOT booking__is_instant GROUP BY metric_time__day -) subq_11 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , delayed_bookings * 2 AS double_counted_delayed_bookings + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , delayed_bookings + FROM cm_4_cte cm_4_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , double_counted_delayed_bookings AS double_counted_delayed_bookings +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql index f956ae8c4..91a2ea57e 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_metric_with_measures_from_multiple_sources_no_dimensions test_filename: test_query_rendering.py sql_engine: Postgres --- --- Combine Aggregated Outputs --- Compute Metrics via Expressions -SELECT - CAST(MAX(subq_15.bookings) AS DOUBLE PRECISION) / CAST(NULLIF(MAX(subq_20.listings), 0) AS DOUBLE PRECISION) AS bookings_per_listing -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings',] @@ -15,8 +12,9 @@ FROM ( SELECT SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 -) subq_15 -CROSS JOIN ( +) + +, cm_7_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['listings',] @@ -25,4 +23,18 @@ CROSS JOIN ( SELECT SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 -) subq_20 +) + +, cm_8_cte AS ( + -- Combine Aggregated Outputs + -- Compute Metrics via Expressions + SELECT + CAST(MAX(cm_6_cte.bookings) AS DOUBLE PRECISION) / CAST(NULLIF(MAX(cm_7_cte.listings), 0) AS DOUBLE PRECISION) AS bookings_per_listing + FROM cm_6_cte cm_6_cte + CROSS JOIN + cm_7_cte cm_7_cte +) + +SELECT + bookings_per_listing AS bookings_per_listing +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_multiple_metrics_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_multiple_metrics_no_dimensions__plan0_optimized.sql index 3e92a120c..11e08ea2c 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_multiple_metrics_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_multiple_metrics_no_dimensions__plan0_optimized.sql @@ -3,10 +3,7 @@ test_filename: test_query_rendering.py sql_engine: Postgres --- -- Combine Aggregated Outputs -SELECT - MAX(subq_17.bookings) AS bookings - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_4_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -17,8 +14,9 @@ FROM ( SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-01' -) subq_17 -CROSS JOIN ( +) + +, cm_5_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -29,4 +27,11 @@ CROSS JOIN ( SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 WHERE DATE_TRUNC('day', created_at) BETWEEN '2020-01-01' AND '2020-01-01' -) subq_23 +) + +SELECT + MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +CROSS JOIN + cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_measure_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_measure_constraint__plan0_optimized.sql index b7754d372..72d8ce81e 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_measure_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_measure_constraint__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_measure_constraint test_filename: test_query_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_12 +WITH cm_11_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_35.metric_time__day, subq_40.metric_time__day) AS metric_time__day - , MAX(subq_35.average_booking_value) AS average_booking_value - , MAX(subq_35.bookings) AS bookings - , MAX(subq_40.booking_value) AS booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_27.metric_time__day AS metric_time__day + , subq_27.bookings AS bookings + , subq_27.average_booking_value AS average_booking_value FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_27.metric_time__day AS metric_time__day - , subq_27.bookings AS bookings - , subq_27.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_27 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_27.listing = listings_latest_src_28000.listing_id - ) subq_31 - WHERE listing__is_lux_latest - GROUP BY - metric_time__day - ) subq_35 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_27 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_27.listing = listings_latest_src_28000.listing_id + ) subq_31 + WHERE listing__is_lux_latest + GROUP BY + metric_time__day +) + +, cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_12_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr + FROM ( + -- Combine Aggregated Outputs SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_11_cte.metric_time__day, cm_10_cte.metric_time__day) AS metric_time__day + , MAX(cm_11_cte.average_booking_value) AS average_booking_value + , MAX(cm_11_cte.bookings) AS bookings + , MAX(cm_10_cte.booking_value) AS booking_value + FROM cm_11_cte cm_11_cte + FULL OUTER JOIN + cm_10_cte cm_10_cte + ON + cm_11_cte.metric_time__day = cm_10_cte.metric_time__day GROUP BY - DATE_TRUNC('day', ds) - ) subq_40 - ON - subq_35.metric_time__day = subq_40.metric_time__day - GROUP BY - COALESCE(subq_35.metric_time__day, subq_40.metric_time__day) -) subq_41 + COALESCE(cm_11_cte.metric_time__day, cm_10_cte.metric_time__day) + ) subq_41 +) + +SELECT + metric_time__day AS metric_time__day + , lux_booking_value_rate_expr AS lux_booking_value_rate_expr +FROM cm_12_cte cm_12_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_measure_constraint_with_reused_measure__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_measure_constraint_with_reused_measure__plan0_optimized.sql index bc2d18516..a5bf9def3 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_measure_constraint_with_reused_measure__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_measure_constraint_with_reused_measure__plan0_optimized.sql @@ -2,52 +2,65 @@ test_name: test_measure_constraint_with_reused_measure test_filename: test_query_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(booking_value_with_is_instant_constraint AS DOUBLE PRECISION) / CAST(NULLIF(booking_value, 0) AS DOUBLE PRECISION) AS instant_booking_value_ratio -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint - , MAX(subq_22.booking_value) AS booking_value + metric_time__day + , SUM(booking_value) AS booking_value_with_is_instant_constraint FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(booking_value) AS booking_value_with_is_instant_constraint - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value + , is_instant AS booking__is_instant + , booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 GROUP BY - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) -) subq_23 + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(booking_value_with_is_instant_constraint AS DOUBLE PRECISION) / CAST(NULLIF(booking_value, 0) AS DOUBLE PRECISION) AS instant_booking_value_ratio + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint + , MAX(cm_7_cte.booking_value) AS booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_value_ratio AS instant_booking_value_ratio +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql index 24fc0afc5..223d225ef 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_measure_constraint_with_single_expr_and_alias test_filename: test_query_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , delayed_bookings * 2 AS double_counted_delayed_bookings -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -26,4 +23,23 @@ FROM ( WHERE NOT booking__is_instant GROUP BY metric_time__day -) subq_11 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , delayed_bookings * 2 AS double_counted_delayed_bookings + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , delayed_bookings + FROM cm_4_cte cm_4_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , double_counted_delayed_bookings AS double_counted_delayed_bookings +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql index ba39460c0..a316d223a 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_metric_with_measures_from_multiple_sources_no_dimensions test_filename: test_query_rendering.py sql_engine: Redshift --- --- Combine Aggregated Outputs --- Compute Metrics via Expressions -SELECT - CAST(MAX(subq_15.bookings) AS DOUBLE PRECISION) / CAST(NULLIF(MAX(subq_20.listings), 0) AS DOUBLE PRECISION) AS bookings_per_listing -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings',] @@ -15,8 +12,9 @@ FROM ( SELECT SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 -) subq_15 -CROSS JOIN ( +) + +, cm_7_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['listings',] @@ -25,4 +23,18 @@ CROSS JOIN ( SELECT SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 -) subq_20 +) + +, cm_8_cte AS ( + -- Combine Aggregated Outputs + -- Compute Metrics via Expressions + SELECT + CAST(MAX(cm_6_cte.bookings) AS DOUBLE PRECISION) / CAST(NULLIF(MAX(cm_7_cte.listings), 0) AS DOUBLE PRECISION) AS bookings_per_listing + FROM cm_6_cte cm_6_cte + CROSS JOIN + cm_7_cte cm_7_cte +) + +SELECT + bookings_per_listing AS bookings_per_listing +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_multiple_metrics_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_multiple_metrics_no_dimensions__plan0_optimized.sql index b56c02e16..1cbfdd46f 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_multiple_metrics_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_multiple_metrics_no_dimensions__plan0_optimized.sql @@ -3,10 +3,7 @@ test_filename: test_query_rendering.py sql_engine: Redshift --- -- Combine Aggregated Outputs -SELECT - MAX(subq_17.bookings) AS bookings - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_4_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -17,8 +14,9 @@ FROM ( SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-01' -) subq_17 -CROSS JOIN ( +) + +, cm_5_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -29,4 +27,11 @@ CROSS JOIN ( SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 WHERE DATE_TRUNC('day', created_at) BETWEEN '2020-01-01' AND '2020-01-01' -) subq_23 +) + +SELECT + MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +CROSS JOIN + cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_measure_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_measure_constraint__plan0_optimized.sql index ade62236e..cc108961f 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_measure_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_measure_constraint__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_measure_constraint test_filename: test_query_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_12 +WITH cm_11_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_35.metric_time__day, subq_40.metric_time__day) AS metric_time__day - , MAX(subq_35.average_booking_value) AS average_booking_value - , MAX(subq_35.bookings) AS bookings - , MAX(subq_40.booking_value) AS booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_27.metric_time__day AS metric_time__day + , subq_27.bookings AS bookings + , subq_27.average_booking_value AS average_booking_value FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_27.metric_time__day AS metric_time__day - , subq_27.bookings AS bookings - , subq_27.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_27 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_27.listing = listings_latest_src_28000.listing_id - ) subq_31 - WHERE listing__is_lux_latest - GROUP BY - metric_time__day - ) subq_35 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_27 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_27.listing = listings_latest_src_28000.listing_id + ) subq_31 + WHERE listing__is_lux_latest + GROUP BY + metric_time__day +) + +, cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_12_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr + FROM ( + -- Combine Aggregated Outputs SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_11_cte.metric_time__day, cm_10_cte.metric_time__day) AS metric_time__day + , MAX(cm_11_cte.average_booking_value) AS average_booking_value + , MAX(cm_11_cte.bookings) AS bookings + , MAX(cm_10_cte.booking_value) AS booking_value + FROM cm_11_cte cm_11_cte + FULL OUTER JOIN + cm_10_cte cm_10_cte + ON + cm_11_cte.metric_time__day = cm_10_cte.metric_time__day GROUP BY - DATE_TRUNC('day', ds) - ) subq_40 - ON - subq_35.metric_time__day = subq_40.metric_time__day - GROUP BY - COALESCE(subq_35.metric_time__day, subq_40.metric_time__day) -) subq_41 + COALESCE(cm_11_cte.metric_time__day, cm_10_cte.metric_time__day) + ) subq_41 +) + +SELECT + metric_time__day AS metric_time__day + , lux_booking_value_rate_expr AS lux_booking_value_rate_expr +FROM cm_12_cte cm_12_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_measure_constraint_with_reused_measure__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_measure_constraint_with_reused_measure__plan0_optimized.sql index 34d2c9ea4..8ae213be8 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_measure_constraint_with_reused_measure__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_measure_constraint_with_reused_measure__plan0_optimized.sql @@ -2,52 +2,65 @@ test_name: test_measure_constraint_with_reused_measure test_filename: test_query_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(booking_value_with_is_instant_constraint AS DOUBLE) / CAST(NULLIF(booking_value, 0) AS DOUBLE) AS instant_booking_value_ratio -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint - , MAX(subq_22.booking_value) AS booking_value + metric_time__day + , SUM(booking_value) AS booking_value_with_is_instant_constraint FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(booking_value) AS booking_value_with_is_instant_constraint - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value + , is_instant AS booking__is_instant + , booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 GROUP BY - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) -) subq_23 + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(booking_value_with_is_instant_constraint AS DOUBLE) / CAST(NULLIF(booking_value, 0) AS DOUBLE) AS instant_booking_value_ratio + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint + , MAX(cm_7_cte.booking_value) AS booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_value_ratio AS instant_booking_value_ratio +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql index 5a05e1279..5e3f75cf1 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_measure_constraint_with_single_expr_and_alias test_filename: test_query_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , delayed_bookings * 2 AS double_counted_delayed_bookings -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -26,4 +23,23 @@ FROM ( WHERE NOT booking__is_instant GROUP BY metric_time__day -) subq_11 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , delayed_bookings * 2 AS double_counted_delayed_bookings + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , delayed_bookings + FROM cm_4_cte cm_4_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , double_counted_delayed_bookings AS double_counted_delayed_bookings +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql index c9596e631..0956be1e7 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_metric_with_measures_from_multiple_sources_no_dimensions test_filename: test_query_rendering.py sql_engine: Snowflake --- --- Combine Aggregated Outputs --- Compute Metrics via Expressions -SELECT - CAST(MAX(subq_15.bookings) AS DOUBLE) / CAST(NULLIF(MAX(subq_20.listings), 0) AS DOUBLE) AS bookings_per_listing -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings',] @@ -15,8 +12,9 @@ FROM ( SELECT SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 -) subq_15 -CROSS JOIN ( +) + +, cm_7_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['listings',] @@ -25,4 +23,18 @@ CROSS JOIN ( SELECT SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 -) subq_20 +) + +, cm_8_cte AS ( + -- Combine Aggregated Outputs + -- Compute Metrics via Expressions + SELECT + CAST(MAX(cm_6_cte.bookings) AS DOUBLE) / CAST(NULLIF(MAX(cm_7_cte.listings), 0) AS DOUBLE) AS bookings_per_listing + FROM cm_6_cte cm_6_cte + CROSS JOIN + cm_7_cte cm_7_cte +) + +SELECT + bookings_per_listing AS bookings_per_listing +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_multiple_metrics_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_multiple_metrics_no_dimensions__plan0_optimized.sql index fe083b5cd..9e5a02bcb 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_multiple_metrics_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_multiple_metrics_no_dimensions__plan0_optimized.sql @@ -3,10 +3,7 @@ test_filename: test_query_rendering.py sql_engine: Snowflake --- -- Combine Aggregated Outputs -SELECT - MAX(subq_17.bookings) AS bookings - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_4_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -17,8 +14,9 @@ FROM ( SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-01' -) subq_17 -CROSS JOIN ( +) + +, cm_5_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -29,4 +27,11 @@ CROSS JOIN ( SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 WHERE DATE_TRUNC('day', created_at) BETWEEN '2020-01-01' AND '2020-01-01' -) subq_23 +) + +SELECT + MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +CROSS JOIN + cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_measure_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_measure_constraint__plan0_optimized.sql index b6a5a7744..971722545 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_measure_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_measure_constraint__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_measure_constraint test_filename: test_query_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_12 +WITH cm_11_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_35.metric_time__day, subq_40.metric_time__day) AS metric_time__day - , MAX(subq_35.average_booking_value) AS average_booking_value - , MAX(subq_35.bookings) AS bookings - , MAX(subq_40.booking_value) AS booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_27.metric_time__day AS metric_time__day + , subq_27.bookings AS bookings + , subq_27.average_booking_value AS average_booking_value FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_27.metric_time__day AS metric_time__day - , subq_27.bookings AS bookings - , subq_27.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_27 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_27.listing = listings_latest_src_28000.listing_id - ) subq_31 - WHERE listing__is_lux_latest - GROUP BY - metric_time__day - ) subq_35 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_27 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_27.listing = listings_latest_src_28000.listing_id + ) subq_31 + WHERE listing__is_lux_latest + GROUP BY + metric_time__day +) + +, cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_12_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr + FROM ( + -- Combine Aggregated Outputs SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_11_cte.metric_time__day, cm_10_cte.metric_time__day) AS metric_time__day + , MAX(cm_11_cte.average_booking_value) AS average_booking_value + , MAX(cm_11_cte.bookings) AS bookings + , MAX(cm_10_cte.booking_value) AS booking_value + FROM cm_11_cte cm_11_cte + FULL OUTER JOIN + cm_10_cte cm_10_cte + ON + cm_11_cte.metric_time__day = cm_10_cte.metric_time__day GROUP BY - DATE_TRUNC('day', ds) - ) subq_40 - ON - subq_35.metric_time__day = subq_40.metric_time__day - GROUP BY - COALESCE(subq_35.metric_time__day, subq_40.metric_time__day) -) subq_41 + COALESCE(cm_11_cte.metric_time__day, cm_10_cte.metric_time__day) + ) subq_41 +) + +SELECT + metric_time__day AS metric_time__day + , lux_booking_value_rate_expr AS lux_booking_value_rate_expr +FROM cm_12_cte cm_12_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_measure_constraint_with_reused_measure__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_measure_constraint_with_reused_measure__plan0_optimized.sql index ffb73d97a..186d8c673 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_measure_constraint_with_reused_measure__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_measure_constraint_with_reused_measure__plan0_optimized.sql @@ -2,52 +2,65 @@ test_name: test_measure_constraint_with_reused_measure test_filename: test_query_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(booking_value_with_is_instant_constraint AS DOUBLE) / CAST(NULLIF(booking_value, 0) AS DOUBLE) AS instant_booking_value_ratio -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint - , MAX(subq_22.booking_value) AS booking_value + metric_time__day + , SUM(booking_value) AS booking_value_with_is_instant_constraint FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(booking_value) AS booking_value_with_is_instant_constraint - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value + , is_instant AS booking__is_instant + , booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 GROUP BY - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) -) subq_23 + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(booking_value_with_is_instant_constraint AS DOUBLE) / CAST(NULLIF(booking_value, 0) AS DOUBLE) AS instant_booking_value_ratio + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint + , MAX(cm_7_cte.booking_value) AS booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_value_ratio AS instant_booking_value_ratio +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql index 0752177f5..b8f6d0f9f 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_measure_constraint_with_single_expr_and_alias test_filename: test_query_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , delayed_bookings * 2 AS double_counted_delayed_bookings -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -26,4 +23,23 @@ FROM ( WHERE NOT booking__is_instant GROUP BY metric_time__day -) subq_11 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , delayed_bookings * 2 AS double_counted_delayed_bookings + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , delayed_bookings + FROM cm_4_cte cm_4_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , double_counted_delayed_bookings AS double_counted_delayed_bookings +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql index 20a4074e4..2df9ec6d0 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_metric_with_measures_from_multiple_sources_no_dimensions test_filename: test_query_rendering.py sql_engine: Trino --- --- Combine Aggregated Outputs --- Compute Metrics via Expressions -SELECT - CAST(MAX(subq_15.bookings) AS DOUBLE) / CAST(NULLIF(MAX(subq_20.listings), 0) AS DOUBLE) AS bookings_per_listing -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings',] @@ -15,8 +12,9 @@ FROM ( SELECT SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 -) subq_15 -CROSS JOIN ( +) + +, cm_7_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['listings',] @@ -25,4 +23,18 @@ CROSS JOIN ( SELECT SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 -) subq_20 +) + +, cm_8_cte AS ( + -- Combine Aggregated Outputs + -- Compute Metrics via Expressions + SELECT + CAST(MAX(cm_6_cte.bookings) AS DOUBLE) / CAST(NULLIF(MAX(cm_7_cte.listings), 0) AS DOUBLE) AS bookings_per_listing + FROM cm_6_cte cm_6_cte + CROSS JOIN + cm_7_cte cm_7_cte +) + +SELECT + bookings_per_listing AS bookings_per_listing +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_multiple_metrics_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_multiple_metrics_no_dimensions__plan0_optimized.sql index 47112df5d..3ece8d630 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_multiple_metrics_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_multiple_metrics_no_dimensions__plan0_optimized.sql @@ -3,10 +3,7 @@ test_filename: test_query_rendering.py sql_engine: Trino --- -- Combine Aggregated Outputs -SELECT - MAX(subq_17.bookings) AS bookings - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_4_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -17,8 +14,9 @@ FROM ( SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 WHERE DATE_TRUNC('day', ds) BETWEEN timestamp '2020-01-01' AND timestamp '2020-01-01' -) subq_17 -CROSS JOIN ( +) + +, cm_5_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -29,4 +27,11 @@ CROSS JOIN ( SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 WHERE DATE_TRUNC('day', created_at) BETWEEN timestamp '2020-01-01' AND timestamp '2020-01-01' -) subq_23 +) + +SELECT + MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +CROSS JOIN + cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_rendered_query.py/str/BigQuery/test_id_enumeration__query.sql b/tests_metricflow/snapshots/test_rendered_query.py/str/BigQuery/test_id_enumeration__query.sql index 3c7a4e472..55e79e4c2 100644 --- a/tests_metricflow/snapshots/test_rendered_query.py/str/BigQuery/test_id_enumeration__query.sql +++ b/tests_metricflow/snapshots/test_rendered_query.py/str/BigQuery/test_id_enumeration__query.sql @@ -3,11 +3,7 @@ test_filename: test_rendered_query.py sql_engine: BigQuery --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) AS metric_time__day - , MAX(subq_4.bookings) AS bookings - , MAX(subq_9.listings) AS listings -FROM ( +WITH cm_2_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -24,8 +20,9 @@ FROM ( ) subq_2 GROUP BY metric_time__day -) subq_4 -FULL OUTER JOIN ( +) + +, cm_3_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -42,8 +39,16 @@ FULL OUTER JOIN ( ) subq_7 GROUP BY metric_time__day -) subq_9 +) + +SELECT + COALESCE(cm_2_cte.metric_time__day, cm_3_cte.metric_time__day) AS metric_time__day + , MAX(cm_2_cte.bookings) AS bookings + , MAX(cm_3_cte.listings) AS listings +FROM cm_2_cte cm_2_cte +FULL OUTER JOIN + cm_3_cte cm_3_cte ON - subq_4.metric_time__day = subq_9.metric_time__day + cm_2_cte.metric_time__day = cm_3_cte.metric_time__day GROUP BY metric_time__day diff --git a/tests_metricflow/snapshots/test_rendered_query.py/str/Databricks/test_id_enumeration__query.sql b/tests_metricflow/snapshots/test_rendered_query.py/str/Databricks/test_id_enumeration__query.sql index 5c1346422..4f6f90d73 100644 --- a/tests_metricflow/snapshots/test_rendered_query.py/str/Databricks/test_id_enumeration__query.sql +++ b/tests_metricflow/snapshots/test_rendered_query.py/str/Databricks/test_id_enumeration__query.sql @@ -3,11 +3,7 @@ test_filename: test_rendered_query.py sql_engine: Databricks --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) AS metric_time__day - , MAX(subq_4.bookings) AS bookings - , MAX(subq_9.listings) AS listings -FROM ( +WITH cm_2_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -24,8 +20,9 @@ FROM ( ) subq_2 GROUP BY metric_time__day -) subq_4 -FULL OUTER JOIN ( +) + +, cm_3_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -42,8 +39,16 @@ FULL OUTER JOIN ( ) subq_7 GROUP BY metric_time__day -) subq_9 +) + +SELECT + COALESCE(cm_2_cte.metric_time__day, cm_3_cte.metric_time__day) AS metric_time__day + , MAX(cm_2_cte.bookings) AS bookings + , MAX(cm_3_cte.listings) AS listings +FROM cm_2_cte cm_2_cte +FULL OUTER JOIN + cm_3_cte cm_3_cte ON - subq_4.metric_time__day = subq_9.metric_time__day + cm_2_cte.metric_time__day = cm_3_cte.metric_time__day GROUP BY - COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) + COALESCE(cm_2_cte.metric_time__day, cm_3_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_rendered_query.py/str/Postgres/test_id_enumeration__query.sql b/tests_metricflow/snapshots/test_rendered_query.py/str/Postgres/test_id_enumeration__query.sql index 8b32d1151..8795f616f 100644 --- a/tests_metricflow/snapshots/test_rendered_query.py/str/Postgres/test_id_enumeration__query.sql +++ b/tests_metricflow/snapshots/test_rendered_query.py/str/Postgres/test_id_enumeration__query.sql @@ -3,11 +3,7 @@ test_filename: test_rendered_query.py sql_engine: Postgres --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) AS metric_time__day - , MAX(subq_4.bookings) AS bookings - , MAX(subq_9.listings) AS listings -FROM ( +WITH cm_2_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -24,8 +20,9 @@ FROM ( ) subq_2 GROUP BY metric_time__day -) subq_4 -FULL OUTER JOIN ( +) + +, cm_3_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -42,8 +39,16 @@ FULL OUTER JOIN ( ) subq_7 GROUP BY metric_time__day -) subq_9 +) + +SELECT + COALESCE(cm_2_cte.metric_time__day, cm_3_cte.metric_time__day) AS metric_time__day + , MAX(cm_2_cte.bookings) AS bookings + , MAX(cm_3_cte.listings) AS listings +FROM cm_2_cte cm_2_cte +FULL OUTER JOIN + cm_3_cte cm_3_cte ON - subq_4.metric_time__day = subq_9.metric_time__day + cm_2_cte.metric_time__day = cm_3_cte.metric_time__day GROUP BY - COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) + COALESCE(cm_2_cte.metric_time__day, cm_3_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_rendered_query.py/str/Redshift/test_id_enumeration__query.sql b/tests_metricflow/snapshots/test_rendered_query.py/str/Redshift/test_id_enumeration__query.sql index 27a52649d..3e897c4f0 100644 --- a/tests_metricflow/snapshots/test_rendered_query.py/str/Redshift/test_id_enumeration__query.sql +++ b/tests_metricflow/snapshots/test_rendered_query.py/str/Redshift/test_id_enumeration__query.sql @@ -3,11 +3,7 @@ test_filename: test_rendered_query.py sql_engine: Redshift --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) AS metric_time__day - , MAX(subq_4.bookings) AS bookings - , MAX(subq_9.listings) AS listings -FROM ( +WITH cm_2_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -24,8 +20,9 @@ FROM ( ) subq_2 GROUP BY metric_time__day -) subq_4 -FULL OUTER JOIN ( +) + +, cm_3_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -42,8 +39,16 @@ FULL OUTER JOIN ( ) subq_7 GROUP BY metric_time__day -) subq_9 +) + +SELECT + COALESCE(cm_2_cte.metric_time__day, cm_3_cte.metric_time__day) AS metric_time__day + , MAX(cm_2_cte.bookings) AS bookings + , MAX(cm_3_cte.listings) AS listings +FROM cm_2_cte cm_2_cte +FULL OUTER JOIN + cm_3_cte cm_3_cte ON - subq_4.metric_time__day = subq_9.metric_time__day + cm_2_cte.metric_time__day = cm_3_cte.metric_time__day GROUP BY - COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) + COALESCE(cm_2_cte.metric_time__day, cm_3_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_rendered_query.py/str/Snowflake/test_id_enumeration__query.sql b/tests_metricflow/snapshots/test_rendered_query.py/str/Snowflake/test_id_enumeration__query.sql index 004108598..69a46525f 100644 --- a/tests_metricflow/snapshots/test_rendered_query.py/str/Snowflake/test_id_enumeration__query.sql +++ b/tests_metricflow/snapshots/test_rendered_query.py/str/Snowflake/test_id_enumeration__query.sql @@ -3,11 +3,7 @@ test_filename: test_rendered_query.py sql_engine: Snowflake --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) AS metric_time__day - , MAX(subq_4.bookings) AS bookings - , MAX(subq_9.listings) AS listings -FROM ( +WITH cm_2_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -24,8 +20,9 @@ FROM ( ) subq_2 GROUP BY metric_time__day -) subq_4 -FULL OUTER JOIN ( +) + +, cm_3_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -42,8 +39,16 @@ FULL OUTER JOIN ( ) subq_7 GROUP BY metric_time__day -) subq_9 +) + +SELECT + COALESCE(cm_2_cte.metric_time__day, cm_3_cte.metric_time__day) AS metric_time__day + , MAX(cm_2_cte.bookings) AS bookings + , MAX(cm_3_cte.listings) AS listings +FROM cm_2_cte cm_2_cte +FULL OUTER JOIN + cm_3_cte cm_3_cte ON - subq_4.metric_time__day = subq_9.metric_time__day + cm_2_cte.metric_time__day = cm_3_cte.metric_time__day GROUP BY - COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) + COALESCE(cm_2_cte.metric_time__day, cm_3_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_rendered_query.py/str/Trino/test_id_enumeration__query.sql b/tests_metricflow/snapshots/test_rendered_query.py/str/Trino/test_id_enumeration__query.sql index aa7df86eb..884253f0f 100644 --- a/tests_metricflow/snapshots/test_rendered_query.py/str/Trino/test_id_enumeration__query.sql +++ b/tests_metricflow/snapshots/test_rendered_query.py/str/Trino/test_id_enumeration__query.sql @@ -3,11 +3,7 @@ test_filename: test_rendered_query.py sql_engine: Trino --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) AS metric_time__day - , MAX(subq_4.bookings) AS bookings - , MAX(subq_9.listings) AS listings -FROM ( +WITH cm_2_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -24,8 +20,9 @@ FROM ( ) subq_2 GROUP BY metric_time__day -) subq_4 -FULL OUTER JOIN ( +) + +, cm_3_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -42,8 +39,16 @@ FULL OUTER JOIN ( ) subq_7 GROUP BY metric_time__day -) subq_9 +) + +SELECT + COALESCE(cm_2_cte.metric_time__day, cm_3_cte.metric_time__day) AS metric_time__day + , MAX(cm_2_cte.bookings) AS bookings + , MAX(cm_3_cte.listings) AS listings +FROM cm_2_cte cm_2_cte +FULL OUTER JOIN + cm_3_cte cm_3_cte ON - subq_4.metric_time__day = subq_9.metric_time__day + cm_2_cte.metric_time__day = cm_3_cte.metric_time__day GROUP BY - COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) + COALESCE(cm_2_cte.metric_time__day, cm_3_cte.metric_time__day)