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

compile --no-inject-ephemeral-ctes flag #8482

Merged
merged 4 commits into from
Sep 7, 2023
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/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):
MichelleArk marked this conversation as resolved.
Show resolved Hide resolved
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
Loading