Skip to content

Commit

Permalink
Use model alias for the CTE identifier generated during ephemeral mat…
Browse files Browse the repository at this point in the history
…erialization (#10290)

* Use alias instead of name when adding ephemeral model prefixes

* Adjust TestCustomSchemaWithCustomMacroFromModelName to test ephemeral models

* Add changelog entry for ephemeral model CTE identifier fix

* Reference model.identifier and model.name where appropriate to resolve typing errors

* Move test for ephemeral model with alias to dedicated test in test_compile.py
  • Loading branch information
jeancochrane authored Aug 9, 2024
1 parent 374412a commit 63262e9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
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"
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

0 comments on commit 63262e9

Please sign in to comment.