{% macro is_unique(columns, where) %}
  {% if columns is string %}
    {% set columns = [columns] %}
  {% endif %}
  WITH non_unique AS (
    SELECT
      COUNT(*) AS total_count
    FROM
      `{{ project_id }}.{{ dataset_id }}.{{ table_name }}`
    {% if where %}
    WHERE {{ where }}
    {% endif %}
    GROUP BY
    {% for col in columns -%}
    {{ col }}{% if not loop.last %},{% endif %}
    {% endfor -%}
    HAVING total_count > 1
  )
  SELECT IF(
   (SELECT COUNT(*) FROM non_unique) > 0,
   ERROR("Duplicates detected (Expected combined set of values for columns {{ columns }} to be unique{% if where %} where {{ where }}{% endif %}.)"),
   NULL
  );
{% endmacro %}
