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

fix #7413: inject sql header in query for show #7568

Merged
merged 1 commit into from
May 9, 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
fix #7413: inject sql header in query for show
  • Loading branch information
aranke committed May 9, 2023
commit bd3867716fe7e0dd3e2cbe13d65fcb79f34a86a5
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
Copy link
Contributor

@iknox-fa iknox-fa May 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost certainly a premature optimization, but it's worth pointing out that string concatenation with the + operator is a bit inefficient.

ERm.. if those are strings. I assume they are at this point in the compilation process.

)

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