Skip to content

Commit

Permalink
fix #7413: inject sql header in query for show (#7568)
Browse files Browse the repository at this point in the history
  • Loading branch information
aranke authored May 9, 2023
1 parent d34c511 commit 272beb2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20230509-102932.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: inject sql header in query for show
time: 2023-05-09T10:29:32.12947-07:00
custom:
Author: aranke
Issue: "7413"
5 changes: 5 additions & 0 deletions core/dbt/task/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ def execute(self, compiled_node, manifest):
# Allow passing in -1 (or any negative number) to get all rows
limit = None if self.config.args.limit < 0 else self.config.args.limit

if "sql_header" in compiled_node.unrendered_config:
compiled_node.compiled_code = (
compiled_node.unrendered_config["sql_header"] + compiled_node.compiled_code
)

adapter_response, execute_result = self.adapter.execute(
compiled_node.compiled_code, fetch=True, limit=limit
)
Expand Down
8 changes: 8 additions & 0 deletions tests/functional/show/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
from {{ ref('sample_model') }}
"""

models__sql_header = """
{% call set_sql_header(config) %}
set session time zone 'Asia/Kolkata';
{%- endcall %}
select current_setting('timezone') as timezone
"""


schema_yml = """
models:
- name: sample_model
Expand Down
7 changes: 7 additions & 0 deletions tests/functional/show/test_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
models__second_model,
models__ephemeral_model,
schema_yml,
models__sql_header,
)


Expand All @@ -19,6 +20,7 @@ def models(self):
"sample_model.sql": models__sample_model,
"second_model.sql": models__second_model,
"ephemeral_model.sql": models__ephemeral_model,
"sql_header.sql": models__sql_header,
}

@pytest.fixture(scope="class")
Expand Down Expand Up @@ -105,6 +107,11 @@ def test_seed(self, project):
(results, log_output) = run_dbt_and_capture(["show", "--select", "sample_seed"])
assert "Previewing node 'sample_seed'" in log_output

def test_sql_header(self, project):
run_dbt(["build"])
(results, log_output) = run_dbt_and_capture(["show", "--select", "sql_header"])
assert "Asia/Kolkata" in log_output


class TestShowModelVersions:
@pytest.fixture(scope="class")
Expand Down

0 comments on commit 272beb2

Please sign in to comment.