Skip to content

Commit

Permalink
compile --no-inject-ephemeral-ctes flag (#8482)
Browse files Browse the repository at this point in the history
(cherry picked from commit e24a952)
  • Loading branch information
benmosher authored and github-actions[bot] committed Sep 11, 2023
1 parent e0f8112 commit b65334f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20230823-140407.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Add --no-inject-ephemeral-ctes flag for `compile` command, for usage by linting.
time: 2023-08-23T14:04:07.617476-04:00
custom:
Author: benmosher
Issue: "8480"
1 change: 1 addition & 0 deletions core/dbt/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ def docs_serve(ctx, **kwargs):
@p.state
@p.defer_state
@p.deprecated_state
@p.compile_inject_ephemeral_ctes
@p.target
@p.target_path
@p.threads
Expand Down
8 changes: 8 additions & 0 deletions core/dbt/cli/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@
default=True,
)

compile_inject_ephemeral_ctes = click.option(
"--inject-ephemeral-ctes/--no-inject-ephemeral-ctes",
envvar=None,
help="Internal flag controlling injection of referenced ephemeral models' CTEs during `compile`.",
hidden=True,
default=True,
)

config_dir = click.option(
"--config-dir",
envvar=None,
Expand Down
4 changes: 4 additions & 0 deletions core/dbt/compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ def _recursively_prepend_ctes(
if model.compiled_code is None:
raise DbtRuntimeError("Cannot inject ctes into an uncompiled node", model)

# tech debt: safe flag/arg access (#6259)
if not getattr(self.config.args, "inject_ephemeral_ctes", True):
return (model, [])

# extra_ctes_injected flag says that we've already recursively injected the ctes
if model.extra_ctes_injected:
return (model, model.extra_ctes)
Expand Down
24 changes: 22 additions & 2 deletions tests/functional/materializations/test_ephemeral_compilation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from dbt.contracts.graph.nodes import ModelNode
from dbt.contracts.results import RunExecutionResult, RunResult
import pytest
from dbt.tests.util import run_dbt

Expand Down Expand Up @@ -53,6 +55,16 @@
"""


SUPPRESSED_CTE_EXPECTED_OUTPUT = """-- fct_eph_first.sql
with int_eph_first as(
select * from __dbt__cte__int_eph_first
)
select * from int_eph_first"""


class TestEphemeralCompilation:
@pytest.fixture(scope="class")
def models(self):
Expand All @@ -67,5 +79,13 @@ def test_ephemeral_compilation(self, project):
results = run_dbt(["run"])
assert len(results) == 0

results = run_dbt(["test"])
len(results) == 4
def test__suppress_injected_ctes(self, project):
compile_output = run_dbt(
["compile", "--no-inject-ephemeral-ctes", "--select", "fct_eph_first"]
)
assert isinstance(compile_output, RunExecutionResult)
node_result = compile_output.results[0]
assert isinstance(node_result, RunResult)
node = node_result.node
assert isinstance(node, ModelNode)
assert node.compiled_code == SUPPRESSED_CTE_EXPECTED_OUTPUT

0 comments on commit b65334f

Please sign in to comment.