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 (#236)
  • Loading branch information
jeancochrane authored Aug 9, 2024
1 parent 2bdf580 commit 313d4a5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20240610-195300.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-10T19:53:00.086488231Z
custom:
Author: jeancochrane
Issue: "5273"
7 changes: 5 additions & 2 deletions dbt/adapters/base/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,11 @@ def create_ephemeral_from(
relation_config: RelationConfig,
limit: Optional[int] = None,
) -> Self:
# Note that ephemeral models are based on the name.
identifier = cls.add_ephemeral_prefix(relation_config.name)
# Note that ephemeral models are based on the identifier, which will
# point to the model's alias if one exists and otherwise fall back to
# the filename. This is intended to give the user more control over
# the way that the CTE name is constructed
identifier = cls.add_ephemeral_prefix(relation_config.identifier)
return cls.create(
type=cls.CTE,
identifier=identifier,
Expand Down
15 changes: 14 additions & 1 deletion tests/unit/test_relation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dataclasses import replace
from dataclasses import dataclass, replace

import pytest

Expand Down Expand Up @@ -79,3 +79,16 @@ def test_render_limited(limit, require_alias, expected_result):
actual_result = my_relation.render_limited()
assert actual_result == expected_result
assert str(my_relation) == expected_result


def test_create_ephemeral_from_uses_identifier():
@dataclass
class Node:
"""Dummy implementation of RelationConfig protocol"""

name: str
identifier: str

node = Node(name="name_should_not_be_used", identifier="test")
ephemeral_relation = BaseRelation.create_ephemeral_from(node)
assert str(ephemeral_relation) == "__dbt__cte__test"

0 comments on commit 313d4a5

Please sign in to comment.