Skip to content

Commit

Permalink
Fix issue with AliasSpecsNode changing column order
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb committed Dec 20, 2024
1 parent 76b1c03 commit f5451bf
Show file tree
Hide file tree
Showing 31 changed files with 148 additions and 152 deletions.
4 changes: 3 additions & 1 deletion metricflow/dataflow/builder/dataflow_plan_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
82 changes: 38 additions & 44 deletions metricflow/plan_conversion/dataflow_to_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit f5451bf

Please sign in to comment.