Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use model alias for the CTE identifier generated during ephemeral materialization #10290

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20240610-200522.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Use model alias for the CTE identifier generated during ephemeral materialization
time: 2024-06-10T20:05:22.510814008Z
custom:
Author: jeancochrane
Issue: "5273"
2 changes: 1 addition & 1 deletion core/dbt/compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@

_extend_prepended_ctes(prepended_ctes, new_prepended_ctes)

new_cte_name = self.add_ephemeral_prefix(cte_model.name)
new_cte_name = self.add_ephemeral_prefix(cte_model.identifier)

Check warning on line 374 in core/dbt/compilation.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/compilation.py#L374

Added line #L374 was not covered by tests
rendered_sql = cte_model._pre_injected_sql or cte_model.compiled_code
sql = f" {new_cte_name} as (\n{rendered_sql}\n)"

Expand Down
9 changes: 9 additions & 0 deletions tests/functional/compile/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@
select sum(n) from t;
"""

first_ephemeral_model_with_alias_sql = """
{{ config(materialized = 'ephemeral', alias = 'first_alias') }}
select 1 as fun
"""

second_ephemeral_model_with_alias_sql = """
select * from {{ ref('first_ephemeral_model_with_alias') }}
"""

schema_yml = """
version: 2

Expand Down
20 changes: 20 additions & 0 deletions tests/functional/compile/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
from tests.functional.assertions.test_runner import dbtTestRunner
from tests.functional.compile.fixtures import (
first_ephemeral_model_sql,
first_ephemeral_model_with_alias_sql,
first_model_sql,
model_multiline_jinja,
schema_yml,
second_ephemeral_model_sql,
second_ephemeral_model_with_alias_sql,
second_model_sql,
third_ephemeral_model_sql,
with_recursive_model_sql,
Expand Down Expand Up @@ -128,6 +130,24 @@ def test_with_recursive_cte(self, project):
]


class TestEphemeralModelWithAlias:
@pytest.fixture(scope="class")
def models(self):
return {
"first_ephemeral_model_with_alias.sql": first_ephemeral_model_with_alias_sql,
"second_ephemeral_model_with_alias.sql": second_ephemeral_model_with_alias_sql,
}

def test_compile(self, project):
run_dbt(["compile"])

assert get_lines("second_ephemeral_model_with_alias") == [
"with __dbt__cte__first_alias as (",
"select 1 as fun",
") select * from __dbt__cte__first_alias",
]


class TestCompile:
@pytest.fixture(scope="class")
def models(self):
Expand Down
Loading