Skip to content

Commit

Permalink
override test sql macro and add limit-placer macro
Browse files Browse the repository at this point in the history
  • Loading branch information
BentsiLeviav committed Jan 28, 2024
1 parent 2b81b76 commit 2b0ba19
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions dbt/include/clickhouse/macros/utils/utils.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
{% macro clickhouse__get_test_sql(main_sql, fail_calc, warn_if, error_if, limit) -%}
{% set main_sql_formatted = clickhouse__place_limit(main_sql, limit) if limit !=None else main_sql%}
select
{{ fail_calc }} as failures,
{{ fail_calc }} {{ warn_if }} as should_warn,
{{ fail_calc }} {{ error_if }} as should_error
from (
{{ main_sql_formatted }}
) dbt_internal_test

{%- endmacro %}


-- This macro is designed to add a LIMIT clause to a ClickHouse SQL query while preserving any ClickHouse settings specified in the query.
-- When multiple queries are nested, the limit will be attached to the outer query
{% macro clickhouse__place_limit(query, limit) -%}
{% if 'settings' in query.lower()%}
{% if '-- settings_section' not in query.lower()%}
{{exceptions.raise_compiler_error("-- settings_section must be set when using ClickHouse settings")}}
{% endif %}
{% set split_by_settings_sections = query.split("-- settings_section")%}
{% set split_by_settings_sections_with_limit = split_by_settings_sections[-2] + "\n LIMIT " + limit|string + "\n" %}
{% set query_with_limit = "-- settings_section".join(split_by_settings_sections[:-2] + [split_by_settings_sections_with_limit, split_by_settings_sections[-1]])%}
{{query_with_limit}}
{% else %}
{{query}}
{{"limit " ~ limit}}
{% endif %}
{%- endmacro %}

{% macro clickhouse__any_value(expression) -%}
any({{ expression }})
{%- endmacro %}
Expand Down

0 comments on commit 2b0ba19

Please sign in to comment.