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 #236

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-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"
ChenyuLInx marked this conversation as resolved.
Show resolved Hide resolved
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"
Loading