Skip to content
Open
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
7 changes: 5 additions & 2 deletions macros/sql/get_column_values.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@

{# Not all relations are tables. Renaming for internal clarity without breaking functionality for anyone using named arguments #}
{# TODO: Change the method signature in a future 0.x.0 release #}
{%- set target_relation = table -%}

{%- set target_relation = table -%}
{% if table is string %}
{{ log("target_relation" ~ target_relation ~ "is not in the valid database.schema.table format. Returning the default value: " ~ default, info=True) }}
{{ return(default) }}
{% endif %}
{# adapter.load_relation is a convenience wrapper to avoid building a Relation when we already have one #}
{% set relation_exists = (load_relation(target_relation)) is not none %}

Expand Down
9 changes: 8 additions & 1 deletion macros/sql/get_query_results_as_dict.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@
{%- if execute -%}
{% set sql_results_table = load_result('get_query_results').table.columns %}
{% for column_name, column in sql_results_table.items() %}
{% do sql_results.update({column_name: column.values()}) %}
{% if column.values() %}
{% do sql_results.update({column_name: column.values()}) %}
{% else %}
{# If column is empty, assign a dummy value#}
{% do sql_results.update({column_name: ' '}) %}
{{ log('Column ' ~ column_name ~ ' has no values. Assigning empty value', info=True) }}
{% endif %}

{% endfor %}
{%- endif -%}

Expand Down
44 changes: 27 additions & 17 deletions macros/sql/pivot.sql
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,32 @@ Arguments:
else_value=0,
quote_identifiers=True,
distinct=False) %}
{% for value in values %}
{{ agg }}(
{% if distinct %} distinct {% endif %}
case
when {{ column }} {{ cmp }} '{{ dbt.escape_single_quotes(value) }}'
then {{ then_value }}
else {{ else_value }}
end
)
{% if alias %}
{% if quote_identifiers %}
as {{ adapter.quote(prefix ~ value ~ suffix) }}
{% else %}
as {{ dbt_utils.slugify(prefix ~ value ~ suffix) }}

{# Check if values are empty or None #}
{% if values is none or values == [] %}
{{ log('Pivot: No values to pivot. Creating an empty column with the default then_value.', info=True) }}
{% set empty_column = prefix ~ 'empty' ~ suffix %}
{{ then_value }} as {{ adapter.quote(empty_column) }}

{% else %}
{# Regular pivot behavior with provided values #}
{% for value in values %}
{{ agg }}(
{% if distinct %} distinct {% endif %}
case
when {{ column }} {{ cmp }} '{{ dbt.escape_single_quotes(value) }}'
then {{ then_value }}
else {{ else_value }}
end
)
{% if alias %}
{% if quote_identifiers %}
as {{ adapter.quote(prefix ~ value ~ suffix) }}
{% else %}
as {{ dbt_utils.slugify(prefix ~ value ~ suffix) }}
{% endif %}
{% endif %}
{% endif %}
{% if not loop.last %},{% endif %}
{% endfor %}
{% if not loop.last %},{% endif %}
{% endfor %}
{% endif %}
{% endmacro %}
Loading