From f14bd2a305450358f24b73cbdb4548b19e119513 Mon Sep 17 00:00:00 2001 From: Simo Tumelius Date: Tue, 22 Aug 2023 13:32:35 +0300 Subject: [PATCH 1/4] Add print_table macro --- macros/print_table.sql | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 macros/print_table.sql diff --git a/macros/print_table.sql b/macros/print_table.sql new file mode 100644 index 0000000..6ca261f --- /dev/null +++ b/macros/print_table.sql @@ -0,0 +1,31 @@ +{% macro print_table(table, max_rows=none, max_columns=13, max_column_width=30, max_precision=none) %} + +{# Check if macro is called in dbt Cloud #} +{%- set is_dbt_cloud = flags.WHICH == 'rpc' -%} + +{%- if not is_dbt_cloud -%} + {% do results.print_table(max_rows=max_rows, max_columns=max_columns, max_column_width=max_column_width, max_precision=max_precision) %} +{% else %} + {%- set table_printout_lines = [] -%} + + {# Get header from column names #} + {%- set headers = results.column_names | list -%} + {%- set horizontal_lines = ['---'] * headers | length -%} + {% do table_printout_lines.append('| ' ~ headers | join(' | ') ~ ' |') %} + {% do table_printout_lines.append('| ' ~ horizontal_lines | join(' | ') ~ ' |') %} + + {# Get row values #} + {% for row in results.rows %} + {%- set list_row = [''] -%} + {% for val in row.values() %} + {% do list_row.append(val) %} + {% endfor %} + {% do table_printout_lines.append(list_row | join(' | ') ~ ' |') %} + {% endfor %} + + {%- set table_printout = table_printout_lines | join ('\n') -%} + {{ print(table_printout) }} +{% endif %} + + +{% endmacro %} \ No newline at end of file From 46b09ef674996b8a5b8a32142a15d6fd3ed9e2cc Mon Sep 17 00:00:00 2001 From: Simo Tumelius Date: Tue, 22 Aug 2023 13:33:55 +0300 Subject: [PATCH 2/4] Change print_profile_docs now uses the print_table macro --- macros/print_profile_docs.sql | 50 +++-------------------------------- 1 file changed, 3 insertions(+), 47 deletions(-) diff --git a/macros/print_profile_docs.sql b/macros/print_profile_docs.sql index 098f4f8..2fbc17b 100644 --- a/macros/print_profile_docs.sql +++ b/macros/print_profile_docs.sql @@ -11,54 +11,10 @@ {%- set startdocs = '{% docs ' ~ docs_name ~ ' %}' -%} {%- set enddocs = '{% enddocs %}' -%} - {# Check if macro is called in dbt Cloud? #} - {%- if flags.WHICH == 'rpc' -%} - {%- set is_dbt_cloud = true -%} - {%- else -%} - {%- set is_dbt_cloud = false -%} - {%- endif -%} + {{ print(startdocs) }} + {{ dbt_profiler.print_table(results, max_rows=max_rows, max_columns=max_columns, max_column_width=max_column_width, max_precision=max_precision) }} + {{ print(enddocs) }} - {% if not is_dbt_cloud %} - - {{ print(startdocs) }} - {% do results.print_table(max_rows=max_rows, max_columns=max_columns, max_column_width=max_column_width, max_precision=max_precision) %} - {{ print(enddocs) }} - - {% else %} - - {%- set profile_docs=[] -%} - {% do profile_docs.append(startdocs) -%} - {% do profile_docs.append('') %} - - {# Get header from column names #} - {%- set headers = results.column_names -%} - {%- set header = [] -%} - {%- set horizontal_line = [] -%} - - {% for i in range(0,headers|length) %} - {% do header.append(headers[i]) %} - {% do horizontal_line.append('---') %} - {% endfor %} - {% do profile_docs.append('| ' ~ header|join(' | ') ~ ' |') %} - {% do profile_docs.append('| ' ~ horizontal_line|join(' | ') ~ ' |') %} - - {# Get row values #} - {% for row in results.rows %} - {%- set list_row = [''] -%} - {% for val in row.values() %} - {% do list_row.append(val) %} - {% endfor %} - {% do profile_docs.append(list_row|join(' | ') ~ ' |') %} - {% endfor %} - {% do profile_docs.append('') %} - {% do profile_docs.append(enddocs) %} - - {# Join profile docs #} - {%- set joined = profile_docs | join ('\n') -%} - {{ log(joined, info=True) }} - {% do return(joined) %} - - {% endif %} {% endif %} From 7577a27cc387ea3418b5f29b7e6c398e10e3b77b Mon Sep 17 00:00:00 2001 From: Simo Tumelius Date: Tue, 22 Aug 2023 13:34:44 +0300 Subject: [PATCH 3/4] Change print_profile now uses the print_table macro --- macros/print_profile.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/macros/print_profile.sql b/macros/print_profile.sql index 9580c37..b7fa448 100644 --- a/macros/print_profile.sql +++ b/macros/print_profile.sql @@ -3,7 +3,7 @@ {%- set results = dbt_profiler.get_profile_table(relation=relation, relation_name=relation_name, schema=schema, database=database, exclude_measures=exclude_measures, include_columns=include_columns, exclude_columns=exclude_columns, where_clause=where_clause) -%} {% if execute %} - {% do results.print_table(max_rows=max_rows, max_columns=max_columns, max_column_width=max_column_width, max_precision=max_precision) %} + {{ dbt_profiler.print_table(results, max_rows=max_rows, max_columns=max_columns, max_column_width=max_column_width, max_precision=max_precision) }} {% endif %} {% endmacro %} \ No newline at end of file From 2bbaa03032878538e6f1eed2fc31533b49d06098 Mon Sep 17 00:00:00 2001 From: Simo Tumelius Date: Tue, 22 Aug 2023 13:35:19 +0300 Subject: [PATCH 4/4] Update README --- README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/README.md b/README.md index e887c30..a70a363 100644 --- a/README.md +++ b/README.md @@ -178,8 +178,6 @@ Call this macro from another macro or dbt model: ## print_profile ([source](macros/print_profile.sql)) -❗ **This macro does not work in dbt Cloud. The profile doesn't display in the cloud console log because the underlying [print_table()](https://agate.readthedocs.io/en/1.6.1/api/table.html#agate.Table.print_table) method is disabled.** - This macro prints a relation profile as a Markdown table to `stdout`. ### Arguments @@ -337,8 +335,6 @@ This what the profile looks like on the dbt docs site: ## print_profile_docs ([source](macros/print_profile_docs.sql)) -❗ **This macro does not work in dbt Cloud. The profile doesn't display in the cloud console log because the underlying [print_table()](https://agate.readthedocs.io/en/1.6.1/api/table.html#agate.Table.print_table) method is disabled.** - This macro prints a relation profile as a Markdown table wrapped in a Jinja `docs` macro to `stdout`. ### Arguments