From f5451bfc1029d4d2c9fc551ab6572d309396d12d Mon Sep 17 00:00:00 2001 From: Courtney Holcomb Date: Fri, 20 Dec 2024 12:51:30 -0800 Subject: [PATCH] Fix issue with AliasSpecsNode changing column order --- .../dataflow/builder/dataflow_plan_builder.py | 4 +- metricflow/plan_conversion/dataflow_to_sql.py | 82 +++++++++---------- ..._metric_with_non_default_grains__plan0.sql | 32 ++++---- ...th_non_default_grains__plan0_optimized.sql | 20 ++--- ..._metric_grouped_by_custom_grain__plan0.sql | 4 +- ..._with_custom_granularity_filter__plan0.sql | 8 +- ...nularity_filter_not_in_group_by__plan0.sql | 6 +- ...nth_dimension_and_offset_window__plan0.sql | 4 +- ...h_offset_window_and_granularity__plan0.sql | 2 +- ...offset_to_grain_and_granularity__plan0.sql | 4 +- ...ry_have_different_granularities__plan0.sql | 2 +- ...n_metric_multiple_granularities__plan0.sql | 4 +- ...ry_have_different_granularities__plan0.sql | 2 +- ...w_metric_multiple_granularities__plan0.sql | 4 +- ...ine_with_filter_not_in_group_by__plan0.sql | 8 +- ..._not_in_group_by_using_agg_time__plan0.sql | 10 +-- ..._using_agg_time_and_metric_time__plan0.sql | 8 +- ...th_filter_smaller_than_group_by__plan0.sql | 10 +-- ...smaller_than_group_by__plan0_optimized.sql | 4 +- ...join_to_time_spine_with_filters__plan0.sql | 12 +-- ..._simple_fill_nulls_with_0_month__plan0.sql | 4 +- ...st_offset_window_with_date_part__plan0.sql | 2 +- ...ily_time_constraint_with_metric__plan0.sql | 4 +- ...est_derived_metric_alias__query_output.txt | 18 ++-- .../test_metric_alias__query_output.txt | 18 ++-- .../test_derived_metric_alias__plan0.sql | 4 +- ..._derived_metric_alias__plan0_optimized.sql | 4 +- .../DuckDB/test_metric_alias__plan0.sql | 4 +- .../test_metric_alias__plan0_optimized.sql | 4 +- ...e_with_input_measure_constraint__plan0.sql | 4 +- ...ne_with_queried_time_constraint__plan0.sql | 4 +- 31 files changed, 148 insertions(+), 152 deletions(-) diff --git a/metricflow/dataflow/builder/dataflow_plan_builder.py b/metricflow/dataflow/builder/dataflow_plan_builder.py index 6f65742d6..feef39cee 100644 --- a/metricflow/dataflow/builder/dataflow_plan_builder.py +++ b/metricflow/dataflow/builder/dataflow_plan_builder.py @@ -901,7 +901,9 @@ def build_sink_node( ) alias_specs = tuple( - SpecToAlias(MetricSpec(metric.element_name), metric) for metric in metric_specs if metric.alias is not None + SpecToAlias(MetricSpec(metric.element_name), MetricSpec(metric.element_name, alias=metric.alias)) + for metric in metric_specs + if metric.alias is not None ) if len(alias_specs) > 0: pre_result_node = AliasSpecsNode.create( diff --git a/metricflow/plan_conversion/dataflow_to_sql.py b/metricflow/plan_conversion/dataflow_to_sql.py index 821ac10f7..442527d12 100644 --- a/metricflow/plan_conversion/dataflow_to_sql.py +++ b/metricflow/plan_conversion/dataflow_to_sql.py @@ -2,7 +2,7 @@ import datetime as dt import logging -from collections import OrderedDict +from collections import OrderedDict, defaultdict from typing import Callable, Dict, FrozenSet, List, Optional, Sequence, Set, Tuple, TypeVar from dbt_semantic_interfaces.enum_extension import assert_values_exhausted @@ -1529,53 +1529,47 @@ def visit_alias_specs_node(self, node: AliasSpecsNode) -> SqlDataSet: # noqa: D parent_data_set = node.parent_node.accept(self) parent_alias = self._next_unique_table_alias() - new_instances: Tuple[MdoInstance, ...] = () - new_select_columns: Tuple[SqlSelectColumn, ...] = () - instances_to_remove_from_parent: Set[MdoInstance] = set() - for spec_to_alias in node.change_specs: - old_spec = spec_to_alias.input_spec - new_spec = spec_to_alias.output_spec - - # Find the instance in the parent data set with matching grain & date part. - old_instance = parent_data_set.instance_for_column_name( - self._column_association_resolver.resolve_spec(old_spec).column_name - ) - - # Build new instance & select column to match requested spec. - new_instance = old_instance.with_new_spec( - new_spec=new_spec, column_association_resolver=self._column_association_resolver - ) - new_expr = SqlColumnReferenceExpression.from_table_and_column_names( - table_alias=parent_alias, column_name=old_instance.associated_column.column_name - ) - new_select_column = SqlSelectColumn(expr=new_expr, column_alias=new_instance.associated_column.column_name) - instances_to_remove_from_parent.add(old_instance) - new_instances += (new_instance,) - new_select_columns += (new_select_column,) - - # Build full output instance set. - filtered_parent_instance_set = group_instances_by_type( - tuple( - instance - for instance in parent_data_set.instance_set.as_tuple - if instance not in instances_to_remove_from_parent - ) - ) - new_instance_set = group_instances_by_type(new_instances) - transformed_instance_set = InstanceSet.merge([filtered_parent_instance_set, new_instance_set]) - - # Build final select columns. - filtered_parent_select_columns = create_simple_select_columns_for_instance_sets( - column_resolver=self._column_association_resolver, - table_alias_to_instance_set=OrderedDict({parent_alias: filtered_parent_instance_set}), - ) - transformed_select_columns = new_select_columns + filtered_parent_select_columns + input_specs_to_output_specs: Dict[InstanceSpec, List[InstanceSpec]] = defaultdict(list) + for change_spec in node.change_specs: + input_specs_to_output_specs[change_spec.input_spec].append(change_spec.output_spec) + + # Build output instances & select columns. + output_instances: Tuple[MdoInstance, ...] = () + output_select_columns: Tuple[SqlSelectColumn, ...] = () + for parent_instance in parent_data_set.instance_set.as_tuple: + if parent_instance.spec in input_specs_to_output_specs: + # If an alias was requested, bild new instance & select column to match requested spec. + new_specs = input_specs_to_output_specs[parent_instance.spec] + for new_spec in new_specs: + new_instance = parent_instance.with_new_spec( + new_spec=new_spec, column_association_resolver=self._column_association_resolver + ) + new_select_column = SqlSelectColumn( + expr=SqlColumnReferenceExpression.from_table_and_column_names( + table_alias=parent_alias, column_name=parent_instance.associated_column.column_name + ), + column_alias=new_instance.associated_column.column_name, + ) + output_instances += (new_instance,) + output_select_columns += (new_select_column,) + else: + # Keep the instance the same and build a column that just references the parent column. + output_instances += (parent_instance,) + column_name = parent_instance.associated_column.column_name + output_select_columns += ( + SqlSelectColumn( + expr=SqlColumnReferenceExpression.from_table_and_column_names( + table_alias=parent_alias, column_name=column_name + ), + column_alias=column_name, + ), + ) return SqlDataSet( - instance_set=transformed_instance_set, + instance_set=group_instances_by_type(output_instances), sql_select_node=SqlSelectStatementNode.create( description=node.description, - select_columns=transformed_select_columns, + select_columns=output_select_columns, from_source=parent_data_set.checked_sql_select_node, from_source_alias=parent_alias, ), diff --git a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlPlan/DuckDB/test_window_metric_with_non_default_grains__plan0.sql b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlPlan/DuckDB/test_window_metric_with_non_default_grains__plan0.sql index b8dee179a..9d55e9c26 100644 --- a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlPlan/DuckDB/test_window_metric_with_non_default_grains__plan0.sql +++ b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlPlan/DuckDB/test_window_metric_with_non_default_grains__plan0.sql @@ -8,47 +8,47 @@ sql_engine: DuckDB --- -- Re-aggregate Metric via Group By SELECT - subq_12.booking__ds__month - , subq_12.metric_time__week + subq_12.metric_time__week + , subq_12.booking__ds__month , subq_12.every_two_days_bookers_fill_nulls_with_0 FROM ( -- Window Function for Metric Re-aggregation SELECT - subq_11.booking__ds__month - , subq_11.metric_time__week + subq_11.metric_time__week + , subq_11.booking__ds__month , FIRST_VALUE(subq_11.every_two_days_bookers_fill_nulls_with_0) OVER ( PARTITION BY - subq_11.booking__ds__month - , subq_11.metric_time__week + subq_11.metric_time__week + , subq_11.booking__ds__month ORDER BY subq_11.metric_time__day ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS every_two_days_bookers_fill_nulls_with_0 FROM ( -- Compute Metrics via Expressions SELECT - subq_10.booking__ds__month + subq_10.metric_time__day , subq_10.metric_time__week - , subq_10.metric_time__day + , subq_10.booking__ds__month , COALESCE(subq_10.bookers, 0) AS every_two_days_bookers_fill_nulls_with_0 FROM ( -- Join to Time Spine Dataset SELECT - subq_9.booking__ds__month AS booking__ds__month + subq_9.metric_time__day AS metric_time__day , subq_9.metric_time__week AS metric_time__week - , subq_9.metric_time__day AS metric_time__day + , subq_9.booking__ds__month AS booking__ds__month , subq_6.bookers AS bookers FROM ( -- Pass Only Elements: ['booking__ds__month', 'metric_time__week', 'metric_time__day'] SELECT - subq_8.booking__ds__month + subq_8.metric_time__day , subq_8.metric_time__week - , subq_8.metric_time__day + , subq_8.booking__ds__month FROM ( -- Change Column Aliases SELECT - subq_7.ds__month AS booking__ds__month + subq_7.ds__day AS metric_time__day , subq_7.ds__week AS metric_time__week - , subq_7.ds__day AS metric_time__day + , subq_7.ds__month AS booking__ds__month , subq_7.ds__quarter , subq_7.ds__year , subq_7.ds__extract_year @@ -414,6 +414,6 @@ FROM ( ) subq_11 ) subq_12 GROUP BY - subq_12.booking__ds__month - , subq_12.metric_time__week + subq_12.metric_time__week + , subq_12.booking__ds__month , subq_12.every_two_days_bookers_fill_nulls_with_0 diff --git a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlPlan/DuckDB/test_window_metric_with_non_default_grains__plan0_optimized.sql b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlPlan/DuckDB/test_window_metric_with_non_default_grains__plan0_optimized.sql index e22a6a683..813a5c427 100644 --- a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlPlan/DuckDB/test_window_metric_with_non_default_grains__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlPlan/DuckDB/test_window_metric_with_non_default_grains__plan0_optimized.sql @@ -8,28 +8,28 @@ sql_engine: DuckDB --- -- Re-aggregate Metric via Group By SELECT - booking__ds__month - , metric_time__week + metric_time__week + , booking__ds__month , every_two_days_bookers_fill_nulls_with_0 FROM ( -- Compute Metrics via Expressions -- Window Function for Metric Re-aggregation SELECT - booking__ds__month - , metric_time__week + metric_time__week + , booking__ds__month , FIRST_VALUE(COALESCE(bookers, 0)) OVER ( PARTITION BY - booking__ds__month - , metric_time__week + metric_time__week + , booking__ds__month ORDER BY metric_time__day ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS every_two_days_bookers_fill_nulls_with_0 FROM ( -- Join to Time Spine Dataset SELECT - DATE_TRUNC('month', time_spine_src_28006.ds) AS booking__ds__month + time_spine_src_28006.ds AS metric_time__day , DATE_TRUNC('week', time_spine_src_28006.ds) AS metric_time__week - , time_spine_src_28006.ds AS metric_time__day + , DATE_TRUNC('month', time_spine_src_28006.ds) AS booking__ds__month , subq_19.bookers AS bookers FROM ***************************.mf_time_spine time_spine_src_28006 LEFT OUTER JOIN ( @@ -60,6 +60,6 @@ FROM ( ) subq_23 ) subq_25 GROUP BY - booking__ds__month - , metric_time__week + metric_time__week + , booking__ds__month , every_two_days_bookers_fill_nulls_with_0 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_join_to_time_spine_metric_grouped_by_custom_grain__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_join_to_time_spine_metric_grouped_by_custom_grain__plan0.sql index c97759a07..8da69f58b 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_join_to_time_spine_metric_grouped_by_custom_grain__plan0.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_join_to_time_spine_metric_grouped_by_custom_grain__plan0.sql @@ -18,8 +18,7 @@ FROM ( FROM ( -- Change Column Aliases SELECT - subq_5.ds__martian_day AS metric_time__martian_day - , subq_5.ds__day AS metric_time__day + subq_5.ds__day AS metric_time__day , subq_5.ds__week , subq_5.ds__month , subq_5.ds__quarter @@ -30,6 +29,7 @@ FROM ( , subq_5.ds__extract_day , subq_5.ds__extract_dow , subq_5.ds__extract_doy + , subq_5.ds__martian_day AS metric_time__martian_day FROM ( -- Read From Time Spine 'mf_time_spine' SELECT diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_join_to_timespine_metric_with_custom_granularity_filter__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_join_to_timespine_metric_with_custom_granularity_filter__plan0.sql index 710baa9d8..8c1303b80 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_join_to_timespine_metric_with_custom_granularity_filter__plan0.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_join_to_timespine_metric_with_custom_granularity_filter__plan0.sql @@ -18,7 +18,8 @@ FROM ( FROM ( -- Constrain Output with WHERE SELECT - subq_7.ds__week + subq_7.metric_time__day + , subq_7.ds__week , subq_7.ds__month , subq_7.ds__quarter , subq_7.ds__year @@ -29,12 +30,10 @@ FROM ( , subq_7.ds__extract_dow , subq_7.ds__extract_doy , subq_7.metric_time__martian_day - , subq_7.metric_time__day FROM ( -- Change Column Aliases SELECT - subq_6.ds__martian_day AS metric_time__martian_day - , subq_6.ds__day AS metric_time__day + subq_6.ds__day AS metric_time__day , subq_6.ds__week , subq_6.ds__month , subq_6.ds__quarter @@ -45,6 +44,7 @@ FROM ( , subq_6.ds__extract_day , subq_6.ds__extract_dow , subq_6.ds__extract_doy + , subq_6.ds__martian_day AS metric_time__martian_day FROM ( -- Read From Time Spine 'mf_time_spine' SELECT diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_join_to_timespine_metric_with_custom_granularity_filter_not_in_group_by__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_join_to_timespine_metric_with_custom_granularity_filter_not_in_group_by__plan0.sql index 156e5cb5e..f60a3d48e 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_join_to_timespine_metric_with_custom_granularity_filter_not_in_group_by__plan0.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_join_to_timespine_metric_with_custom_granularity_filter_not_in_group_by__plan0.sql @@ -18,7 +18,8 @@ FROM ( FROM ( -- Constrain Output with WHERE SELECT - subq_7.ds__week + subq_7.metric_time__day + , subq_7.ds__week , subq_7.ds__month , subq_7.ds__quarter , subq_7.ds__year @@ -28,13 +29,11 @@ FROM ( , subq_7.ds__extract_day , subq_7.ds__extract_dow , subq_7.ds__extract_doy - , subq_7.metric_time__day , subq_7.metric_time__martian_day FROM ( -- Change Column Aliases SELECT subq_6.ds__day AS metric_time__day - , subq_6.ds__martian_day AS metric_time__martian_day , subq_6.ds__week , subq_6.ds__month , subq_6.ds__quarter @@ -45,6 +44,7 @@ FROM ( , subq_6.ds__extract_day , subq_6.ds__extract_dow , subq_6.ds__extract_doy + , subq_6.ds__martian_day AS metric_time__martian_day FROM ( -- Read From Time Spine 'mf_time_spine' SELECT diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_month_dimension_and_offset_window__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_month_dimension_and_offset_window__plan0.sql index 38fb75948..903d96ecb 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_month_dimension_and_offset_window__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_month_dimension_and_offset_window__plan0.sql @@ -52,9 +52,9 @@ FROM ( FROM ( -- Change Column Aliases SELECT - subq_2.ds__month AS metric_time__month - , subq_2.ds__day + subq_2.ds__day , subq_2.ds__week + , subq_2.ds__month AS metric_time__month , subq_2.ds__quarter , subq_2.ds__year , subq_2.ds__extract_year diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window_and_granularity__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window_and_granularity__plan0.sql index 4ede9e08d..0d507fae8 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window_and_granularity__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window_and_granularity__plan0.sql @@ -352,9 +352,9 @@ FROM ( -- Change Column Aliases SELECT subq_7.ds__day AS metric_time__day - , subq_7.ds__quarter AS metric_time__quarter , subq_7.ds__week , subq_7.ds__month + , subq_7.ds__quarter AS metric_time__quarter , subq_7.ds__year , subq_7.ds__extract_year , subq_7.ds__extract_quarter diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0.sql index 1d5827eea..5ddaf7477 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0.sql @@ -137,10 +137,10 @@ FROM ( -- Change Column Aliases SELECT subq_2.ds__day AS metric_time__day - , subq_2.ds__year AS metric_time__year , subq_2.ds__week , subq_2.ds__month , subq_2.ds__quarter + , subq_2.ds__year AS metric_time__year , subq_2.ds__extract_year , subq_2.ds__extract_quarter , subq_2.ds__extract_month @@ -496,10 +496,10 @@ FROM ( -- Change Column Aliases SELECT subq_11.ds__day AS metric_time__day - , subq_11.ds__year AS metric_time__year , subq_11.ds__week , subq_11.ds__month , subq_11.ds__quarter + , subq_11.ds__year AS metric_time__year , subq_11.ds__extract_year , subq_11.ds__extract_quarter , subq_11.ds__extract_month diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0.sql index b0b6ac3fd..492eed4ec 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0.sql @@ -234,8 +234,8 @@ FROM ( -- Change Column Aliases SELECT subq_2.ds__day AS metric_time__day - , subq_2.ds__month AS metric_time__month , subq_2.ds__week + , subq_2.ds__month AS metric_time__month , subq_2.ds__quarter , subq_2.ds__year , subq_2.ds__extract_year diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_to_grain_metric_multiple_granularities__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_to_grain_metric_multiple_granularities__plan0.sql index 6fd81d09c..520924329 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_to_grain_metric_multiple_granularities__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_to_grain_metric_multiple_granularities__plan0.sql @@ -142,10 +142,10 @@ FROM ( -- Change Column Aliases SELECT subq_2.ds__day AS metric_time__day - , subq_2.ds__month AS metric_time__month - , subq_2.ds__year AS metric_time__year , subq_2.ds__week + , subq_2.ds__month AS metric_time__month , subq_2.ds__quarter + , subq_2.ds__year AS metric_time__year , subq_2.ds__extract_year , subq_2.ds__extract_quarter , subq_2.ds__extract_month diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_window_metric_filter_and_query_have_different_granularities__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_window_metric_filter_and_query_have_different_granularities__plan0.sql index 11695bd14..0193f74be 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_window_metric_filter_and_query_have_different_granularities__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_window_metric_filter_and_query_have_different_granularities__plan0.sql @@ -240,8 +240,8 @@ FROM ( -- Change Column Aliases SELECT subq_2.ds__day AS metric_time__day - , subq_2.ds__month AS metric_time__month , subq_2.ds__week + , subq_2.ds__month AS metric_time__month , subq_2.ds__quarter , subq_2.ds__year , subq_2.ds__extract_year diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_window_metric_multiple_granularities__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_window_metric_multiple_granularities__plan0.sql index 7a8245baf..6a740121f 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_window_metric_multiple_granularities__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_window_metric_multiple_granularities__plan0.sql @@ -150,10 +150,10 @@ FROM ( -- Change Column Aliases SELECT subq_2.ds__day AS metric_time__day - , subq_2.ds__month AS metric_time__month - , subq_2.ds__year AS metric_time__year , subq_2.ds__week + , subq_2.ds__month AS metric_time__month , subq_2.ds__quarter + , subq_2.ds__year AS metric_time__year , subq_2.ds__extract_year , subq_2.ds__extract_quarter , subq_2.ds__extract_month diff --git a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_filter_not_in_group_by__plan0.sql b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_filter_not_in_group_by__plan0.sql index ea9c07be1..d3cd92a43 100644 --- a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_filter_not_in_group_by__plan0.sql +++ b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_filter_not_in_group_by__plan0.sql @@ -18,7 +18,9 @@ FROM ( FROM ( -- Constrain Output with WHERE SELECT - subq_6.ds__week + subq_6.metric_time__day + , subq_6.ds__week + , subq_6.metric_time__month , subq_6.ds__quarter , subq_6.ds__year , subq_6.ds__extract_year @@ -28,14 +30,12 @@ FROM ( , subq_6.ds__extract_dow , subq_6.ds__extract_doy , subq_6.ds__martian_day - , subq_6.metric_time__day - , subq_6.metric_time__month FROM ( -- Change Column Aliases SELECT subq_5.ds__day AS metric_time__day - , subq_5.ds__month AS metric_time__month , subq_5.ds__week + , subq_5.ds__month AS metric_time__month , subq_5.ds__quarter , subq_5.ds__year , subq_5.ds__extract_year diff --git a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_filter_not_in_group_by_using_agg_time__plan0.sql b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_filter_not_in_group_by_using_agg_time__plan0.sql index 8de3706f6..0180ffe11 100644 --- a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_filter_not_in_group_by_using_agg_time__plan0.sql +++ b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_filter_not_in_group_by_using_agg_time__plan0.sql @@ -18,7 +18,10 @@ FROM ( FROM ( -- Constrain Output with WHERE SELECT - subq_6.ds__week + subq_6.booking__ds__day + , subq_6.metric_time__day + , subq_6.ds__week + , subq_6.booking__ds__month , subq_6.ds__quarter , subq_6.ds__year , subq_6.ds__extract_year @@ -28,16 +31,13 @@ FROM ( , subq_6.ds__extract_dow , subq_6.ds__extract_doy , subq_6.ds__martian_day - , subq_6.booking__ds__day - , subq_6.metric_time__day - , subq_6.booking__ds__month FROM ( -- Change Column Aliases SELECT subq_5.ds__day AS booking__ds__day , subq_5.ds__day AS metric_time__day - , subq_5.ds__month AS booking__ds__month , subq_5.ds__week + , subq_5.ds__month AS booking__ds__month , subq_5.ds__quarter , subq_5.ds__year , subq_5.ds__extract_year diff --git a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_filter_not_in_group_by_using_agg_time_and_metric_time__plan0.sql b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_filter_not_in_group_by_using_agg_time_and_metric_time__plan0.sql index a40701c93..e13ab8825 100644 --- a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_filter_not_in_group_by_using_agg_time_and_metric_time__plan0.sql +++ b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_filter_not_in_group_by_using_agg_time_and_metric_time__plan0.sql @@ -18,7 +18,9 @@ FROM ( FROM ( -- Constrain Output with WHERE SELECT - subq_6.ds__week + subq_6.metric_time__day + , subq_6.ds__week + , subq_6.booking__ds__month , subq_6.ds__quarter , subq_6.ds__year , subq_6.ds__extract_year @@ -28,14 +30,12 @@ FROM ( , subq_6.ds__extract_dow , subq_6.ds__extract_doy , subq_6.ds__martian_day - , subq_6.metric_time__day - , subq_6.booking__ds__month FROM ( -- Change Column Aliases SELECT subq_5.ds__day AS metric_time__day - , subq_5.ds__month AS booking__ds__month , subq_5.ds__week + , subq_5.ds__month AS booking__ds__month , subq_5.ds__quarter , subq_5.ds__year , subq_5.ds__extract_year diff --git a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_filter_smaller_than_group_by__plan0.sql b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_filter_smaller_than_group_by__plan0.sql index 62fddb8e7..33e2737b0 100644 --- a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_filter_smaller_than_group_by__plan0.sql +++ b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_filter_smaller_than_group_by__plan0.sql @@ -18,7 +18,9 @@ FROM ( FROM ( -- Constrain Output with WHERE SELECT - subq_6.ts__week + subq_6.metric_time__hour + , subq_6.metric_time__day + , subq_6.ts__week , subq_6.ts__month , subq_6.ts__quarter , subq_6.ts__year @@ -28,13 +30,11 @@ FROM ( , subq_6.ts__extract_day , subq_6.ts__extract_dow , subq_6.ts__extract_doy - , subq_6.metric_time__day - , subq_6.metric_time__hour FROM ( -- Change Column Aliases SELECT - subq_5.ts__day AS metric_time__day - , subq_5.ts__hour AS metric_time__hour + subq_5.ts__hour AS metric_time__hour + , subq_5.ts__day AS metric_time__day , subq_5.ts__week , subq_5.ts__month , subq_5.ts__quarter diff --git a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_filter_smaller_than_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_filter_smaller_than_group_by__plan0_optimized.sql index 40962ba36..796df4b8e 100644 --- a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_filter_smaller_than_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_filter_smaller_than_group_by__plan0_optimized.sql @@ -16,8 +16,8 @@ FROM ( -- Read From Time Spine 'mf_time_spine_hour' -- Change Column Aliases SELECT - DATE_TRUNC('day', ts) AS metric_time__day - , ts AS metric_time__hour + ts AS metric_time__hour + , DATE_TRUNC('day', ts) AS metric_time__day FROM ***************************.mf_time_spine_hour time_spine_src_28005 ) subq_16 WHERE (metric_time__hour > '2020-01-01 00:09:00') AND (metric_time__day = '2020-01-01') diff --git a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_filters__plan0.sql b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_filters__plan0.sql index e5e3fae83..645f31182 100644 --- a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_filters__plan0.sql +++ b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_filters__plan0.sql @@ -23,7 +23,9 @@ FROM ( FROM ( -- Constrain Time Range to [2020-01-03T00:00:00, 2020-01-05T00:00:00] SELECT - subq_8.ds__month + subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.ds__month , subq_8.ds__quarter , subq_8.ds__year , subq_8.ds__extract_year @@ -33,12 +35,12 @@ FROM ( , subq_8.ds__extract_dow , subq_8.ds__extract_doy , subq_8.ds__martian_day - , subq_8.metric_time__day - , subq_8.metric_time__week FROM ( -- Constrain Output with WHERE SELECT - subq_7.ds__month + subq_7.metric_time__day + , subq_7.metric_time__week + , subq_7.ds__month , subq_7.ds__quarter , subq_7.ds__year , subq_7.ds__extract_year @@ -48,8 +50,6 @@ FROM ( , subq_7.ds__extract_dow , subq_7.ds__extract_doy , subq_7.ds__martian_day - , subq_7.metric_time__day - , subq_7.metric_time__week FROM ( -- Change Column Aliases SELECT diff --git a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_simple_fill_nulls_with_0_month__plan0.sql b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_simple_fill_nulls_with_0_month__plan0.sql index 81c05fd11..d3a38d45f 100644 --- a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_simple_fill_nulls_with_0_month__plan0.sql +++ b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_simple_fill_nulls_with_0_month__plan0.sql @@ -18,9 +18,9 @@ FROM ( FROM ( -- Change Column Aliases SELECT - subq_4.ds__month AS metric_time__month - , subq_4.ds__day + subq_4.ds__day , subq_4.ds__week + , subq_4.ds__month AS metric_time__month , subq_4.ds__quarter , subq_4.ds__year , subq_4.ds__extract_year diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlPlan/DuckDB/test_offset_window_with_date_part__plan0.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlPlan/DuckDB/test_offset_window_with_date_part__plan0.sql index 81b8d3102..e9ac13162 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlPlan/DuckDB/test_offset_window_with_date_part__plan0.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlPlan/DuckDB/test_offset_window_with_date_part__plan0.sql @@ -352,7 +352,6 @@ FROM ( -- Change Column Aliases SELECT subq_7.ds__day AS metric_time__day - , subq_7.ds__extract_dow AS metric_time__extract_dow , subq_7.ds__week , subq_7.ds__month , subq_7.ds__quarter @@ -361,6 +360,7 @@ FROM ( , subq_7.ds__extract_quarter , subq_7.ds__extract_month , subq_7.ds__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow , subq_7.ds__extract_doy , subq_7.ds__martian_day FROM ( diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlPlan/DuckDB/test_subdaily_time_constraint_with_metric__plan0.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlPlan/DuckDB/test_subdaily_time_constraint_with_metric__plan0.sql index 13336354f..916315972 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlPlan/DuckDB/test_subdaily_time_constraint_with_metric__plan0.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlPlan/DuckDB/test_subdaily_time_constraint_with_metric__plan0.sql @@ -23,7 +23,8 @@ FROM ( FROM ( -- Constrain Time Range to [2020-01-01T02:00:00, 2020-01-01T05:00:00] SELECT - subq_6.ts__day + subq_6.metric_time__hour + , subq_6.ts__day , subq_6.ts__week , subq_6.ts__month , subq_6.ts__quarter @@ -34,7 +35,6 @@ FROM ( , subq_6.ts__extract_day , subq_6.ts__extract_dow , subq_6.ts__extract_doy - , subq_6.metric_time__hour FROM ( -- Change Column Aliases SELECT diff --git a/tests_metricflow/snapshots/test_query_output.py/str/DuckDB/test_derived_metric_alias__query_output.txt b/tests_metricflow/snapshots/test_query_output.py/str/DuckDB/test_derived_metric_alias__query_output.txt index 033c0ec3c..3487a4c2a 100644 --- a/tests_metricflow/snapshots/test_query_output.py/str/DuckDB/test_derived_metric_alias__query_output.txt +++ b/tests_metricflow/snapshots/test_query_output.py/str/DuckDB/test_derived_metric_alias__query_output.txt @@ -1,12 +1,12 @@ test_name: test_derived_metric_alias test_filename: test_query_output.py --- - bookings_alias metric_time__day ----------------- ------------------- - 47.56 2019-12-01T00:00:00 - 284.93 2019-12-18T00:00:00 - 360.5 2019-12-19T00:00:00 - 0 2019-12-20T00:00:00 - 136.16 2020-01-01T00:00:00 - 132.78 2020-01-02T00:00:00 - 0 2020-01-03T00:00:00 +metric_time__day booking_fees +------------------- -------------- +2019-12-01T00:00:00 47.56 +2019-12-18T00:00:00 284.93 +2019-12-19T00:00:00 360.5 +2019-12-20T00:00:00 0 +2020-01-01T00:00:00 136.16 +2020-01-02T00:00:00 132.78 +2020-01-03T00:00:00 0 diff --git a/tests_metricflow/snapshots/test_query_output.py/str/DuckDB/test_metric_alias__query_output.txt b/tests_metricflow/snapshots/test_query_output.py/str/DuckDB/test_metric_alias__query_output.txt index f155e0fe5..78956e89f 100644 --- a/tests_metricflow/snapshots/test_query_output.py/str/DuckDB/test_metric_alias__query_output.txt +++ b/tests_metricflow/snapshots/test_query_output.py/str/DuckDB/test_metric_alias__query_output.txt @@ -1,12 +1,12 @@ test_name: test_metric_alias test_filename: test_query_output.py --- - bookings_alias metric_time__day ----------------- ------------------- - 1 2019-12-01T00:00:00 - 10 2019-12-18T00:00:00 - 18 2019-12-19T00:00:00 - 2 2019-12-20T00:00:00 - 5 2020-01-01T00:00:00 - 9 2020-01-02T00:00:00 - 1 2020-01-03T00:00:00 +metric_time__day bookings +------------------- ---------- +2019-12-01T00:00:00 1 +2019-12-18T00:00:00 10 +2019-12-19T00:00:00 18 +2019-12-20T00:00:00 2 +2020-01-01T00:00:00 5 +2020-01-02T00:00:00 9 +2020-01-03T00:00:00 1 diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlPlan/DuckDB/test_derived_metric_alias__plan0.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlPlan/DuckDB/test_derived_metric_alias__plan0.sql index 2e67d6bbd..0b4c1fda5 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlPlan/DuckDB/test_derived_metric_alias__plan0.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlPlan/DuckDB/test_derived_metric_alias__plan0.sql @@ -6,8 +6,8 @@ sql_engine: DuckDB --- -- Change Column Aliases SELECT - subq_15.booking_fees AS bookings_alias - , subq_15.metric_time__day + subq_15.metric_time__day + , subq_15.booking_fees FROM ( -- Order By ['booking_fees'] SELECT diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlPlan/DuckDB/test_derived_metric_alias__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlPlan/DuckDB/test_derived_metric_alias__plan0_optimized.sql index 710375390..e04e8d30b 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlPlan/DuckDB/test_derived_metric_alias__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlPlan/DuckDB/test_derived_metric_alias__plan0_optimized.sql @@ -17,8 +17,8 @@ WITH sma_28009_cte AS ( ) SELECT - booking_fees AS bookings_alias - , metric_time__day AS metric_time__day + metric_time__day AS metric_time__day + , booking_fees AS booking_fees FROM ( -- Compute Metrics via Expressions SELECT diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlPlan/DuckDB/test_metric_alias__plan0.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlPlan/DuckDB/test_metric_alias__plan0.sql index b99986b3c..6ef9f1446 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlPlan/DuckDB/test_metric_alias__plan0.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlPlan/DuckDB/test_metric_alias__plan0.sql @@ -6,8 +6,8 @@ sql_engine: DuckDB --- -- Change Column Aliases SELECT - subq_13.bookings AS bookings_alias - , subq_13.metric_time__month + subq_13.metric_time__month + , subq_13.bookings FROM ( -- Order By ['bookings'] SELECT diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlPlan/DuckDB/test_metric_alias__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlPlan/DuckDB/test_metric_alias__plan0_optimized.sql index 7c04bb04b..d59ff6b5b 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlPlan/DuckDB/test_metric_alias__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlPlan/DuckDB/test_metric_alias__plan0_optimized.sql @@ -17,8 +17,8 @@ WITH sma_28009_cte AS ( ) SELECT - bookings AS bookings_alias - , metric_time__month AS metric_time__month + metric_time__month AS metric_time__month + , bookings AS bookings FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__month'] diff --git a/tests_metricflow/snapshots/test_time_spine_join_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_input_measure_constraint__plan0.sql b/tests_metricflow/snapshots/test_time_spine_join_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_input_measure_constraint__plan0.sql index 35c4688bc..f29c887bd 100644 --- a/tests_metricflow/snapshots/test_time_spine_join_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_input_measure_constraint__plan0.sql +++ b/tests_metricflow/snapshots/test_time_spine_join_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_input_measure_constraint__plan0.sql @@ -24,7 +24,8 @@ FROM ( FROM ( -- Constrain Output with WHERE SELECT - subq_6.ds__week + subq_6.metric_time__day + , subq_6.ds__week , subq_6.ds__month , subq_6.ds__quarter , subq_6.ds__year @@ -35,7 +36,6 @@ FROM ( , subq_6.ds__extract_dow , subq_6.ds__extract_doy , subq_6.ds__martian_day - , subq_6.metric_time__day FROM ( -- Change Column Aliases SELECT diff --git a/tests_metricflow/snapshots/test_time_spine_join_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_queried_time_constraint__plan0.sql b/tests_metricflow/snapshots/test_time_spine_join_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_queried_time_constraint__plan0.sql index 29b5b3842..aafef7d45 100644 --- a/tests_metricflow/snapshots/test_time_spine_join_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_queried_time_constraint__plan0.sql +++ b/tests_metricflow/snapshots/test_time_spine_join_rendering.py/SqlPlan/DuckDB/test_join_to_time_spine_with_queried_time_constraint__plan0.sql @@ -25,7 +25,8 @@ FROM ( FROM ( -- Constrain Time Range to [2020-01-03T00:00:00, 2020-01-05T00:00:00] SELECT - subq_6.ds__week + subq_6.metric_time__day + , subq_6.ds__week , subq_6.ds__month , subq_6.ds__quarter , subq_6.ds__year @@ -36,7 +37,6 @@ FROM ( , subq_6.ds__extract_dow , subq_6.ds__extract_doy , subq_6.ds__martian_day - , subq_6.metric_time__day FROM ( -- Change Column Aliases SELECT