Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: dbt-labs/metricflow
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: d09b3fa83b534916ff551ad815ad2167f52211fb
Choose a base ref
..
head repository: dbt-labs/metricflow
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 5d323c0cb91968175bdff3747c0f4d33dabc695d
Choose a head ref
2 changes: 1 addition & 1 deletion metricflow/sql/optimizer/table_alias_simplifier.py
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ def visit_cte_node(self, node: SqlCteNode) -> SqlQueryPlanNode:
return node.with_new_select(node.select_statement.accept(self))

def visit_select_statement_node(self, node: SqlSelectStatementNode) -> SqlQueryPlanNode: # noqa: D102
# If there is only a single parent, no table aliases are required since there's no ambiguity.
# If there is only a single source in the SELECT, no table aliases are required since there's no ambiguity.
should_simplify_table_aliases = len(node.join_descs) == 0

if should_simplify_table_aliases:
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
test_name: test_table_alias_no_simplification
test_filename: test_cte_table_alias_simplifier.py
docstring:
Tests that table aliases in the SELECT statement of a CTE are not removed when required.
---
optimizer:
SqlTableAliasSimplifier

sql_before_optimizing:
-- Top-level SELECT
WITH cte_source_0 AS (
-- CTE source 0
SELECT
from_source_alias.col_0 AS cte_source_0__col_0
FROM test_schema.test_table_0 from_source_alias
INNER JOIN
test_schema.test_table_1 right_source_alias
ON
from_source_alias.col_0 = right_source_alias.col_0
)

SELECT
cte_source_0_alias.cte_source_0__col_0 AS top_level__col_0
FROM cte_source_0 cte_source_0_alias

sql_after_optimizing:
-- Top-level SELECT
WITH cte_source_0 AS (
-- CTE source 0
SELECT
from_source_alias.col_0 AS cte_source_0__col_0
FROM test_schema.test_table_0 from_source_alias
INNER JOIN
test_schema.test_table_1 right_source_alias
ON
from_source_alias.col_0 = right_source_alias.col_0
)

SELECT
cte_source_0__col_0 AS top_level__col_0
FROM cte_source_0 cte_source_0_alias
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
test_name: test_table_alias_simplification
test_filename: test_cte_table_alias_simplifier.py
docstring:
Tests that table aliases are removed when not needed in CTEs.
Tests that table aliases in the SELECT statement of a CTE are removed when not needed.
---
optimizer:
SqlTableAliasSimplifier
@@ -44,9 +44,6 @@ sql_before_optimizing:
cte_source_1 right_source_alias
ON
cte_source_0_alias.cte_source_0__col_1 = right_source_alias.right_source__col_1
GROUP BY
cte_source_0_alias.cte_source_0__col_0
, right_source_alias.right_source__col_1

sql_after_optimizing:
-- Top-level SELECT
@@ -86,6 +83,3 @@ sql_after_optimizing:
cte_source_1 right_source_alias
ON
cte_source_0_alias.cte_source_0__col_1 = right_source_alias.right_source__col_1
GROUP BY
cte_source_0_alias.cte_source_0__col_0
, right_source_alias.right_source__col_1
82 changes: 67 additions & 15 deletions tests_metricflow/sql/optimizer/test_cte_table_alias_simplifier.py
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ def test_table_alias_simplification(
mf_test_configuration: MetricFlowTestConfiguration,
sql_plan_renderer: DefaultSqlQueryPlanRenderer,
) -> None:
"""Tests that table aliases are removed when not needed in CTEs."""
"""Tests that table aliases in the SELECT statement of a CTE are removed when not needed."""
select_statement = SqlSelectStatementNode.create(
description="Top-level SELECT",
select_columns=(
@@ -69,20 +69,6 @@ def test_table_alias_simplification(
join_type=SqlJoinType.INNER,
),
),
group_bys=(
SqlSelectColumn(
expr=SqlColumnReferenceExpression.create(
col_ref=SqlColumnReference(table_alias="cte_source_0_alias", column_name="cte_source_0__col_0")
),
column_alias="top_level__col_0",
),
SqlSelectColumn(
expr=SqlColumnReferenceExpression.create(
col_ref=SqlColumnReference(table_alias="right_source_alias", column_name="right_source__col_1")
),
column_alias="top_level__col_1",
),
),
cte_sources=(
SqlCteNode.create(
cte_alias="cte_source_0",
@@ -181,3 +167,69 @@ def test_table_alias_simplification(
sql_plan_renderer=sql_plan_renderer,
select_statement=select_statement,
)


def test_table_alias_no_simplification(
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
sql_plan_renderer: DefaultSqlQueryPlanRenderer,
) -> None:
"""Tests that table aliases in the SELECT statement of a CTE are not removed when required."""
select_statement = SqlSelectStatementNode.create(
description="Top-level SELECT",
select_columns=(
SqlSelectColumn(
expr=SqlColumnReferenceExpression.create(
col_ref=SqlColumnReference(table_alias="cte_source_0_alias", column_name="cte_source_0__col_0")
),
column_alias="top_level__col_0",
),
),
from_source=SqlTableNode.create(sql_table=SqlTable(schema_name=None, table_name="cte_source_0")),
from_source_alias="cte_source_0_alias",
cte_sources=(
SqlCteNode.create(
cte_alias="cte_source_0",
select_statement=SqlSelectStatementNode.create(
description="CTE source 0",
select_columns=(
SqlSelectColumn(
expr=SqlColumnReferenceExpression.create(
col_ref=SqlColumnReference(table_alias="from_source_alias", column_name="col_0")
),
column_alias="cte_source_0__col_0",
),
),
from_source=SqlTableNode.create(
sql_table=SqlTable(schema_name="test_schema", table_name="test_table_0")
),
from_source_alias="from_source_alias",
join_descs=(
SqlJoinDescription(
right_source=SqlTableNode.create(
sql_table=SqlTable(schema_name="test_schema", table_name="test_table_1")
),
right_source_alias="right_source_alias",
on_condition=SqlComparisonExpression.create(
left_expr=SqlColumnReferenceExpression.create(
col_ref=SqlColumnReference(table_alias="from_source_alias", column_name="col_0")
),
comparison=SqlComparison.EQUALS,
right_expr=SqlColumnReferenceExpression.create(
col_ref=SqlColumnReference(table_alias="right_source_alias", column_name="col_0")
),
),
join_type=SqlJoinType.INNER,
),
),
),
),
),
)
assert_optimizer_result_snapshot_equal(
request=request,
mf_test_configuration=mf_test_configuration,
optimizer=SqlTableAliasSimplifier(),
sql_plan_renderer=sql_plan_renderer,
select_statement=select_statement,
)