Skip to content

Commit

Permalink
Change get_profile.group_by is now a list of column names (#66)
Browse files Browse the repository at this point in the history
* Feature: add group_by

* Feature: update README.md

* Change group_by is now a list of column names

* Fix typos

---------

Co-authored-by: xtutran <[email protected]>
Co-authored-by: Simo Tumelius <[email protected]>
  • Loading branch information
3 people authored Feb 9, 2023
1 parent ed09f2e commit f2e11ab
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ This macro returns a relation profile as a SQL query that can be used in a dbt m
* `include_columns` (optional): List of columns to include in the profile (default: `[]` i.e., all). Only one of `include_columns` and `exclude_columns` can be specified at a time.
* `exclude_columns` (optional): List of columns to exclude from the profile (default: `[]`). Only one of `include_columns` and `exclude_columns` can be specified at a time.
* `where_clause` (optional): SQL `WHERE` clause to allow exclustion of records from profiler.
* `group_by` (optional): SQL `group_by` to aggregate data from profiler.
* `group_by` (optional): SQL `group_by` to aggregate data from profiler (default: `[]`)

### Usage

Expand Down
3 changes: 1 addition & 2 deletions integration_tests/models/profile_group_by.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
-- depends_on: {{ ref("test_data_group_by") }}
{% if execute %}
{%- set group_by = "group_by" -%}
{{ dbt_profiler.get_profile(relation=ref("test_data_group_by"), group_by=group_by) }}
{{ dbt_profiler.get_profile(relation=ref("test_data_group_by"), group_by=["group_by"]) }}
{% endif %}
20 changes: 10 additions & 10 deletions macros/get_profile.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{% macro get_profile(relation, exclude_measures=[], include_columns=[], exclude_columns=[], where_clause=none, group_by=none) %}
{% macro get_profile(relation, exclude_measures=[], include_columns=[], exclude_columns=[], where_clause=none, group_by=[]) %}
{{ return(adapter.dispatch("get_profile", macro_namespace="dbt_profiler")(relation, exclude_measures, include_columns, exclude_columns, where_clause, group_by)) }}
{% endmacro %}



{% macro default__get_profile(relation, exclude_measures=[], include_columns=[], exclude_columns=[], where_clause=none, group_by=none) %}
{% macro default__get_profile(relation, exclude_measures=[], include_columns=[], exclude_columns=[], where_clause=none, group_by=[]) %}

{%- if include_columns and exclude_columns -%}
{{ exceptions.raise_compiler_error("Both include_columns and exclude_columns arguments were provided to the `get_profile` macro. Only one is allowed.") }}
Expand Down Expand Up @@ -69,9 +69,9 @@
{% for column_name in profile_column_names %}
{% set data_type = data_type_map.get(column_name.lower(), "") %}
select
{% if group_by %}
{{ group_by }},
{% endif %}
{%- for group_by_column in group_by %}
{{ group_by_column }},
{%- endfor %}
lower('{{ column_name }}') as column_name,
nullif(lower('{{ data_type }}'), '') as data_type,
{% if "row_count" not in exclude_measures -%}
Expand Down Expand Up @@ -114,24 +114,24 @@
{{ loop.index }} as _column_position
from source_data
{% if group_by %}
group by {{ group_by }}, lower('{{ column_name }}')
group by {{ group_by | join(", ") }}
{% endif %}
{% if not loop.last %}union all{% endif %}
{% endfor %}
)

select
{% if group_by %}
{{ group_by }},
{% endif %}
{%- for group_by_column in group_by %}
{{ group_by_column }},
{%- endfor %}
column_name,
data_type,
{% for measure in include_measures %}
{{ measure }},
{% endfor %}
profiled_at
from column_profiles
order by {% if group_by %} {{ group_by }} asc, {% endif %} _column_position asc
order by {% if group_by %}{{ group_by | join(", ") }},{% endif %} _column_position asc
{% endset %}

{% do return(profile_sql) %}
Expand Down

0 comments on commit f2e11ab

Please sign in to comment.