Skip to content

Commit

Permalink
first pass: add limit in show sql
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelleArk committed Sep 13, 2023
1 parent 5182e3c commit 40d6db1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
21 changes: 21 additions & 0 deletions core/dbt/include/global_project/macros/adapters/show.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% macro show(limit) -%}
{%- set sql_header = config.get('sql_header', none) -%}
{{ sql_header if sql_header is not none }}
{%- if limit is not none -%}
{{ get_limit_subquery_sql(compiled_code, limit) }}
{%- else -%}
{{ compiled_code }}
{%- endif -%}
{% endmacro %}

{% macro get_limit_subquery_sql(sql, limit) %}
{{ adapter.dispatch('get_limit_subquery_sql', 'dbt')(sql, limit) }}
{% endmacro %}

{% macro default__get_limit_subquery_sql(sql, limit) %}
select *
from (
{{ sql }}
) as model_limit_subq
limit {{ limit }}
{% endmacro %}
16 changes: 9 additions & 7 deletions core/dbt/task/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import threading
import time

from dbt.context.providers import generate_runtime_model_context
from dbt.contracts.graph.nodes import SeedNode
from dbt.contracts.results import RunResult, RunStatus
from dbt.events.base_types import EventLevel
Expand All @@ -23,14 +24,15 @@ 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
model_context = generate_runtime_model_context(compiled_node, self.config, manifest)
show_limit_sql = self.adapter.execute_macro(

Check warning on line 28 in core/dbt/task/show.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/task/show.py#L27-L28

Added lines #L27 - L28 were not covered by tests
macro_name="show",
manifest=manifest,
context_override=model_context,
kwargs={"limit": limit},
)
adapter_response, execute_result = self.adapter.execute(show_limit_sql, fetch=True)

Check warning on line 34 in core/dbt/task/show.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/task/show.py#L34

Added line #L34 was not covered by tests

end_time = time.time()

return RunResult(
Expand Down

0 comments on commit 40d6db1

Please sign in to comment.