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

remove double underscore from cte prefix ephemeral #2662

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,6 @@ target/

# pycharm
.idea/

# vscode
.history/
8 changes: 4 additions & 4 deletions core/dbt/contracts/graph/compiled.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ def _inject_ctes_into_sql(sql: str, ctes: List[InjectedCTE]) -> str:
[
InjectedCTE(
id="cte_id_1",
sql="__dbt__CTE__ephemeral as (select * from table)",
sql="dbt__CTE__ephemeral as (select * from table)",
),
InjectedCTE(
id="cte_id_2",
sql="__dbt__CTE__events as (select id, type from events)",
sql="dbt__CTE__events as (select id, type from events)",
),
]

Expand All @@ -145,8 +145,8 @@ def _inject_ctes_into_sql(sql: str, ctes: List[InjectedCTE]) -> str:

This will spit out:

"with __dbt__CTE__ephemeral as (select * from table),
__dbt__CTE__events as (select id, type from events),
"with dbt__CTE__ephemeral as (select * from table),
dbt__CTE__events as (select id, type from events),
with internal_cte as (select * from sessions)
select * from internal_cte"

Expand Down
2 changes: 1 addition & 1 deletion core/dbt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def filter_null_values(input: Dict[K_T, Optional[V_T]]) -> Dict[K_T, V_T]:


def add_ephemeral_model_prefix(s: str) -> str:
return '__dbt__CTE__{}'.format(s)
return 'dbt__CTE__{}'.format(s)


def timestring() -> str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2755,17 +2755,17 @@ def expected_postgres_references_run_results(self):
my_schema_name = self.unique_schema()
ephemeral_compiled_sql = (
'\n\nselect first_name, count(*) as ct from '
'__dbt__CTE__ephemeral_copy\ngroup by first_name\n'
'dbt__CTE__ephemeral_copy\ngroup by first_name\n'
'order by first_name asc'
)

cte_sql = (
' __dbt__CTE__ephemeral_copy as (\n\n\nselect * from {}."{}"."seed"\n)'
' dbt__CTE__ephemeral_copy as (\n\n\nselect * from {}."{}"."seed"\n)'
).format(self.default_database, my_schema_name)

ephemeral_injected_sql = (
'\n\nwith{}select first_name, count(*) as ct from '
'__dbt__CTE__ephemeral_copy\ngroup by first_name\n'
'dbt__CTE__ephemeral_copy\ngroup by first_name\n'
'order by first_name asc'
).format(cte_sql)

Expand Down
4 changes: 2 additions & 2 deletions test/integration/048_rpc_test/test_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ def query_url(url, query):
return requests.post(url, headers=headers, data=json.dumps(query))


_select_from_ephemeral = '''with __dbt__CTE__ephemeral_model as (
_select_from_ephemeral = '''with dbt__CTE__ephemeral_model as (


select 1 as id
)select * from __dbt__CTE__ephemeral_model'''
)select * from dbt__CTE__ephemeral_model'''


def addr_in_use(err, *args):
Expand Down
30 changes: 15 additions & 15 deletions test/unit/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def setUp(self):

def mock_generate_runtime_model_context(model, config, manifest):
def ref(name):
result = f'__dbt__CTE__{name}'
result = f'dbt__CTE__{name}'
unique_id = f'model.root.{name}'
model.extra_ctes.append(InjectedCTE(id=unique_id, sql=None))
return result
Expand Down Expand Up @@ -80,7 +80,7 @@ def test__prepend_ctes__already_has_cte(self):
injected_sql='',
compiled_sql=(
'with cte as (select * from something_else) '
'select * from __dbt__CTE__ephemeral')
'select * from dbt__CTE__ephemeral')
),
'model.root.ephemeral': CompiledModelNode(
name='ephemeral',
Expand Down Expand Up @@ -126,10 +126,10 @@ def test__prepend_ctes__already_has_cte(self):
self.assertEqual(result.extra_ctes_injected, True)
self.assertEqualIgnoreWhitespace(
result.injected_sql,
('with __dbt__CTE__ephemeral as ('
('with dbt__CTE__ephemeral as ('
'select * from source_table'
'), cte as (select * from something_else) '
'select * from __dbt__CTE__ephemeral'))
'select * from dbt__CTE__ephemeral'))

self.assertEqual(
manifest.nodes['model.root.ephemeral'].extra_ctes_injected,
Expand Down Expand Up @@ -254,7 +254,7 @@ def test__prepend_ctes(self):
extra_ctes_injected=False,
extra_ctes=[InjectedCTE(id='model.root.ephemeral', sql='select * from source_table')],
injected_sql='',
compiled_sql='select * from __dbt__CTE__ephemeral'
compiled_sql='select * from dbt__CTE__ephemeral'
),
'model.root.ephemeral': CompiledModelNode(
name='ephemeral',
Expand Down Expand Up @@ -301,10 +301,10 @@ def test__prepend_ctes(self):
self.assertTrue(result.extra_ctes_injected)
self.assertEqualIgnoreWhitespace(
result.injected_sql,
('with __dbt__CTE__ephemeral as ('
('with dbt__CTE__ephemeral as ('
'select * from source_table'
') '
'select * from __dbt__CTE__ephemeral'))
'select * from dbt__CTE__ephemeral'))

self.assertTrue(manifest.nodes['model.root.ephemeral'].extra_ctes_injected)

Expand Down Expand Up @@ -378,7 +378,7 @@ def test__prepend_ctes__cte_not_compiled(self):
extra_ctes_injected=False,
extra_ctes=[InjectedCTE(id='model.root.ephemeral', sql='select * from source_table')],
injected_sql='',
compiled_sql='select * from __dbt__CTE__ephemeral'
compiled_sql='select * from dbt__CTE__ephemeral'
),
'model.root.ephemeral': parsed_ephemeral,
},
Expand Down Expand Up @@ -407,10 +407,10 @@ def test__prepend_ctes__cte_not_compiled(self):
self.assertTrue(result.extra_ctes_injected)
self.assertEqualIgnoreWhitespace(
result.injected_sql,
('with __dbt__CTE__ephemeral as ('
('with dbt__CTE__ephemeral as ('
'select * from source_table'
') '
'select * from __dbt__CTE__ephemeral'))
'select * from dbt__CTE__ephemeral'))

self.assertTrue(manifest.nodes['model.root.ephemeral'].extra_ctes_injected)

Expand Down Expand Up @@ -442,7 +442,7 @@ def test__prepend_ctes__multiple_levels(self):
extra_ctes_injected=False,
extra_ctes=[InjectedCTE(id='model.root.ephemeral', sql=None)],
injected_sql=None,
compiled_sql='select * from __dbt__CTE__ephemeral'
compiled_sql='select * from dbt__CTE__ephemeral'
),
'model.root.ephemeral': ParsedModelNode(
name='ephemeral',
Expand Down Expand Up @@ -503,12 +503,12 @@ def test__prepend_ctes__multiple_levels(self):
self.assertTrue(result.extra_ctes_injected)
self.assertEqualIgnoreWhitespace(
result.injected_sql,
('with __dbt__CTE__ephemeral_level_two as ('
('with dbt__CTE__ephemeral_level_two as ('
'select * from source_table'
'), __dbt__CTE__ephemeral as ('
'select * from __dbt__CTE__ephemeral_level_two'
'), dbt__CTE__ephemeral as ('
'select * from dbt__CTE__ephemeral_level_two'
') '
'select * from __dbt__CTE__ephemeral'))
'select * from dbt__CTE__ephemeral'))

self.assertTrue(manifest.nodes['model.root.ephemeral'].compiled)
self.assertTrue(manifest.nodes['model.root.ephemeral_level_two'].compiled)
Expand Down