{% test assert_dimension_completeness(
    model, column_name, metric_column_names, global_dimension_name="global"
) %}

    with

    {% for metric_column_name in metric_column_names %}
        sum_by_dimension_{{ metric_column_name }} as (
            select {{ column_name }}, sum({{ metric_column_name }}) as sum_metric
            from {{ model }}
            group by {{ column_name }}
        ),
        global_sum_{{ metric_column_name }} as (
            select sum({{ metric_column_name }}) as total_metric
            from {{ model }}
            where {{ column_name }} = '{{ global_dimension_name }}'
        ),
        discrepancy_{{ metric_column_name }} as (
            select *
            from sum_by_dimension_{{ metric_column_name }}, global_sum_{{ metric_column_name }}
            where sum_by_dimension_{{ metric_column_name }}.sum_metric != global_sum_{{ metric_column_name }}.total_metric
        )
        {% if not loop.last %}, {% endif %}
    {% endfor %}

    {% for metric_column_name in metric_column_names %}
        {% if loop.first %}
            select * from discrepancy_{{ metric_column_name }}
        {% else %}
            union all
            select * from discrepancy_{{ metric_column_name }}
        {% endif %}
    {% endfor %}

{% endtest %}
