diff --git a/core/dbt/include/global_project/macros/adapters/show.sql b/core/dbt/include/global_project/macros/adapters/show.sql new file mode 100644 index 00000000000..2ff97dd68fe --- /dev/null +++ b/core/dbt/include/global_project/macros/adapters/show.sql @@ -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 %} diff --git a/core/dbt/task/show.py b/core/dbt/task/show.py index f9af847e874..eb52a392757 100644 --- a/core/dbt/task/show.py +++ b/core/dbt/task/show.py @@ -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 @@ -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( + macro_name="show", + manifest=manifest, + context_override=model_context, + kwargs={"limit": limit}, ) + adapter_response, execute_result = self.adapter.execute(show_limit_sql, fetch=True) + end_time = time.time() return RunResult(