From b4b217c79c77459d428e24b8c3e8876ac9cc9184 Mon Sep 17 00:00:00 2001 From: Ben Mosher Date: Wed, 23 Aug 2023 13:47:14 -0400 Subject: [PATCH 1/4] compile --suppress-ephemeral-ctes flag, for inline compilation of models during linting only --- .../unreleased/Features-20230823-140407.yaml | 6 +++++ core/dbt/cli/main.py | 1 + core/dbt/cli/params.py | 8 +++++++ core/dbt/compilation.py | 4 ++++ .../test_ephemeral_compilation.py | 24 +++++++++++++++++-- 5 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 .changes/unreleased/Features-20230823-140407.yaml diff --git a/.changes/unreleased/Features-20230823-140407.yaml b/.changes/unreleased/Features-20230823-140407.yaml new file mode 100644 index 00000000000..0841b3cb77a --- /dev/null +++ b/.changes/unreleased/Features-20230823-140407.yaml @@ -0,0 +1,6 @@ +kind: Features +body: Add --suppress-ephemeral-ctes flag for `compile` command, for usage by linting. +time: 2023-08-23T14:04:07.617476-04:00 +custom: + Author: benmosher + Issue: "8480" diff --git a/core/dbt/cli/main.py b/core/dbt/cli/main.py index b53ff3277ba..f837c832cc9 100644 --- a/core/dbt/cli/main.py +++ b/core/dbt/cli/main.py @@ -330,6 +330,7 @@ def docs_serve(ctx, **kwargs): @p.state @p.defer_state @p.deprecated_state +@p.compile_suppress_ephemeral_ctes @p.target @p.target_path @p.threads diff --git a/core/dbt/cli/params.py b/core/dbt/cli/params.py index 3c016958fb4..1d5a490613b 100644 --- a/core/dbt/cli/params.py +++ b/core/dbt/cli/params.py @@ -40,6 +40,14 @@ default=True, ) +compile_suppress_ephemeral_ctes = click.option( + "--suppress-ephemeral-ctes/--no-suppress-ephemeral-ctes", + envvar="DBT_SUPPRESS_EPHEMERAL_CTES", + help="Internal flag for whether to supress injecting referenced ephemeral models' CTEs during `compile`.", + hidden=True, + default=False, +) + config_dir = click.option( "--config-dir", envvar=None, diff --git a/core/dbt/compilation.py b/core/dbt/compilation.py index 959ad2516d1..92fee92c9b8 100644 --- a/core/dbt/compilation.py +++ b/core/dbt/compilation.py @@ -320,6 +320,10 @@ def _recursively_prepend_ctes( if model.compiled_code is None: raise DbtRuntimeError("Cannot inject ctes into an uncompiled node", model) + flags = get_flags() + if getattr(flags, "SUPPRESS_EPHEMERAL_CTES", False): + 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) diff --git a/tests/functional/materializations/test_ephemeral_compilation.py b/tests/functional/materializations/test_ephemeral_compilation.py index 56f49928756..809df4a47f2 100644 --- a/tests/functional/materializations/test_ephemeral_compilation.py +++ b/tests/functional/materializations/test_ephemeral_compilation.py @@ -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 @@ -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): @@ -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", "--suppress-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 From 79d8e9b0f6d1dff707ea300c388476f46e2a7845 Mon Sep 17 00:00:00 2001 From: Ben Mosher Date: Wed, 6 Sep 2023 08:55:31 -0400 Subject: [PATCH 2/4] get_flags() => self.config.args --- core/dbt/compilation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/dbt/compilation.py b/core/dbt/compilation.py index 92fee92c9b8..f53b999ede1 100644 --- a/core/dbt/compilation.py +++ b/core/dbt/compilation.py @@ -320,8 +320,8 @@ def _recursively_prepend_ctes( if model.compiled_code is None: raise DbtRuntimeError("Cannot inject ctes into an uncompiled node", model) - flags = get_flags() - if getattr(flags, "SUPPRESS_EPHEMERAL_CTES", False): + # tech debt: safe flag/arg access (#6259) + if getattr(self.config.args, "suppress_ephemeral_ctes", False): return (model, []) # extra_ctes_injected flag says that we've already recursively injected the ctes From 4152a242fa32635a5b5b122878c58323fe253059 Mon Sep 17 00:00:00 2001 From: Ben Mosher Date: Wed, 6 Sep 2023 11:24:59 -0400 Subject: [PATCH 3/4] boolean inversion --- core/dbt/cli/main.py | 2 +- core/dbt/cli/params.py | 10 +++++----- core/dbt/compilation.py | 2 +- .../materializations/test_ephemeral_compilation.py | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/core/dbt/cli/main.py b/core/dbt/cli/main.py index f837c832cc9..9b6e8459cb0 100644 --- a/core/dbt/cli/main.py +++ b/core/dbt/cli/main.py @@ -330,7 +330,7 @@ def docs_serve(ctx, **kwargs): @p.state @p.defer_state @p.deprecated_state -@p.compile_suppress_ephemeral_ctes +@p.compile_inject_ephemeral_ctes @p.target @p.target_path @p.threads diff --git a/core/dbt/cli/params.py b/core/dbt/cli/params.py index 1d5a490613b..f7a7365a07e 100644 --- a/core/dbt/cli/params.py +++ b/core/dbt/cli/params.py @@ -40,12 +40,12 @@ default=True, ) -compile_suppress_ephemeral_ctes = click.option( - "--suppress-ephemeral-ctes/--no-suppress-ephemeral-ctes", - envvar="DBT_SUPPRESS_EPHEMERAL_CTES", - help="Internal flag for whether to supress injecting referenced ephemeral models' CTEs during `compile`.", +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=False, + default=True, ) config_dir = click.option( diff --git a/core/dbt/compilation.py b/core/dbt/compilation.py index f53b999ede1..f37e248515a 100644 --- a/core/dbt/compilation.py +++ b/core/dbt/compilation.py @@ -321,7 +321,7 @@ def _recursively_prepend_ctes( raise DbtRuntimeError("Cannot inject ctes into an uncompiled node", model) # tech debt: safe flag/arg access (#6259) - if getattr(self.config.args, "suppress_ephemeral_ctes", False): + 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 diff --git a/tests/functional/materializations/test_ephemeral_compilation.py b/tests/functional/materializations/test_ephemeral_compilation.py index 809df4a47f2..c9f17d3e00c 100644 --- a/tests/functional/materializations/test_ephemeral_compilation.py +++ b/tests/functional/materializations/test_ephemeral_compilation.py @@ -81,7 +81,7 @@ def test_ephemeral_compilation(self, project): def test__suppress_injected_ctes(self, project): compile_output = run_dbt( - ["compile", "--suppress-ephemeral-ctes", "--select", "fct_eph_first"] + ["compile", "--no-inject-ephemeral-ctes", "--select", "fct_eph_first"] ) assert isinstance(compile_output, RunExecutionResult) node_result = compile_output.results[0] From 1235de18c07f5138207979e34c7fecc5391d0bf8 Mon Sep 17 00:00:00 2001 From: Ben Mosher Date: Wed, 6 Sep 2023 11:56:53 -0400 Subject: [PATCH 4/4] changie fixup --- .changes/unreleased/Features-20230823-140407.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changes/unreleased/Features-20230823-140407.yaml b/.changes/unreleased/Features-20230823-140407.yaml index 0841b3cb77a..2b5101ecf11 100644 --- a/.changes/unreleased/Features-20230823-140407.yaml +++ b/.changes/unreleased/Features-20230823-140407.yaml @@ -1,5 +1,5 @@ kind: Features -body: Add --suppress-ephemeral-ctes flag for `compile` command, for usage by linting. +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