Skip to content

Commit

Permalink
Refactor BaseRelation.create_ephemeral_from to use identifier instead…
Browse files Browse the repository at this point in the history
… of alias
  • Loading branch information
jeancochrane committed Jul 16, 2024
1 parent b8508ad commit e5c1b46
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
13 changes: 7 additions & 6 deletions dbt/adapters/base/relation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import re
from collections.abc import Hashable
from dataclasses import dataclass, field
from typing import (
Expand Down Expand Up @@ -233,18 +232,20 @@ def quoted(self, identifier):
)

@staticmethod
def add_ephemeral_prefix(alias: str):
return re.sub(r"\W+", "", f"__dbt__cte__{alias}")
def add_ephemeral_prefix(name: str):
return f"__dbt__cte__{name}"

@classmethod
def create_ephemeral_from(
cls: Type[Self],
relation_config: RelationConfig,
limit: Optional[int] = None,
) -> Self:
# Note that ephemeral models are based on the alias to give the user
# more control over the way that the CTE name is constructed
identifier = cls.add_ephemeral_prefix(relation_config.alias)
# 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
1 change: 0 additions & 1 deletion dbt/adapters/contracts/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def __delitem__(self, key): ...
class RelationConfig(Protocol):
resource_type: str
name: str
alias: str
description: str
database: str
schema: str
Expand Down
18 changes: 6 additions & 12 deletions tests/unit/test_relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

from dbt.adapters.base import BaseRelation
from dbt.adapters.contracts.relation import RelationConfig, RelationType
from dbt.adapters.contracts.relation import RelationType


@pytest.mark.parametrize(
Expand Down Expand Up @@ -81,20 +81,14 @@ def test_render_limited(limit, require_alias, expected_result):
assert str(my_relation) == expected_result


@pytest.mark.parametrize(
"alias,expected_cte_id",
[
("table", "table"),
("test.<>*~!@#$%^&*table", "testtable")
]
)
def test_create_ephemeral_from_uses_alias(alias, expected_cte_id):
def test_create_ephemeral_from_uses_identifier():
@dataclass
class Node:
"""Dummy implementation of RelationConfig protocol"""

name: str
alias: str
identifier: str

node = Node(name="name should not be used", alias=alias)
node = Node(name="name_should_not_be_used", identifier="test")
ephemeral_relation = BaseRelation.create_ephemeral_from(node)
assert str(ephemeral_relation) == f"__dbt__cte__{expected_cte_id}"
assert str(ephemeral_relation) == "__dbt__cte__test"

0 comments on commit e5c1b46

Please sign in to comment.