From 63262e93cb59ed3b5143a1194bc46ba4c03feca1 Mon Sep 17 00:00:00 2001 From: Jean Cochrane Date: Fri, 9 Aug 2024 17:00:55 -0500 Subject: [PATCH] Use model alias for the CTE identifier generated during ephemeral materialization (#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 --- .../unreleased/Fixes-20240610-200522.yaml | 6 ++++++ tests/functional/compile/fixtures.py | 9 +++++++++ tests/functional/compile/test_compile.py | 20 +++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 .changes/unreleased/Fixes-20240610-200522.yaml diff --git a/.changes/unreleased/Fixes-20240610-200522.yaml b/.changes/unreleased/Fixes-20240610-200522.yaml new file mode 100644 index 00000000000..456575644ac --- /dev/null +++ b/.changes/unreleased/Fixes-20240610-200522.yaml @@ -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" diff --git a/tests/functional/compile/fixtures.py b/tests/functional/compile/fixtures.py index e0be7c895bf..97ccd6d16b6 100644 --- a/tests/functional/compile/fixtures.py +++ b/tests/functional/compile/fixtures.py @@ -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 diff --git a/tests/functional/compile/test_compile.py b/tests/functional/compile/test_compile.py index e7732b09a8c..956169eddf1 100644 --- a/tests/functional/compile/test_compile.py +++ b/tests/functional/compile/test_compile.py @@ -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, @@ -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):