Skip to main content

Construct interior

Breaking the inside of an overflowing {{ … }} or {% set %} construct.

Each pair below is a real, passing case from dfmt's test corpus (suites/dfmt-jinja/cases/construct-interior/): the template as written, then the same template after dfmt — every Jinja tag reproduced exactly, the SQL laid out around it. 28 examples.

A closers indent does not decide the lowering​

Before

{% macro f(a, b) -%}
x
{%- endmacro %}
select
a,
{{ f(relations=relations_list,
other=second_thing)
}} as cols
from t

After

{% macro f(a, b) -%}
x
{%- endmacro %}

select a, {{ f(relations = relations_list, other = second_thing) }} as cols
from t

A deeper closer lowers the same way​

Before

{% macro f(a, b) -%}
x
{%- endmacro %}
select
a,
{{ f(relations=relations_list,
other=second_thing)
}} as cols
from t

After

{% macro f(a, b) -%}
x
{%- endmacro %}

select a, {{ f(relations = relations_list, other = second_thing) }} as cols
from t

A dict breaks after its brace like any bracket​

Before

{{ config(
meta = {
"description": "Count of inpatient encounters that are active at any time during each day",
"short_description": "Active encounters",
"primary_resource": "Encounter",
"calculation": "COUNT",
"dimension_b_description": "The organization responsible for providing the services for this encounter",
}
) -}}

select 1 as a

After

{{
config(
meta = {
"description": "Count of inpatient encounters that are active at any time during each day",
"short_description": "Active encounters",
"primary_resource": "Encounter",
"calculation": "COUNT",
"dimension_b_description": "The organization responsible for providing the services for this encounter",
}
)
-}}

select 1 as a

A dict entry too long for its line keeps its key​

Before

{{ config(
meta = {
"short_description": "Active encounters",
"dimension_a_description": "The status of the medication administration (in-progress | not-done | on-hold | completed | entered-in-error)",
}
) -}}

select 1 as a

After

{{
config(
meta = {
"short_description": "Active encounters",
"dimension_a_description": "The status of the medication administration (in-progress | not-done | on-hold | completed | entered-in-error)",
}
)
-}}

select 1 as a

A dict literal hugs its braces​

Before

{{ config(
materialized='incremental',
partition_by = {'field': 'dt', 'data_type': 'date'}
) }}
select a from t

After

{{ config(materialized = 'incremental', partition_by = {'field': 'dt', 'data_type': 'date'}) }}

select a
from t

A fitting tag is spaced like a reflowed one​

Before

select {{ var ("cols") }}, b
from {{ ref ("orders") }}
where c = {{ var ('d' , 'x') }} and e in ({{ ['a','b'] | join (', ') }})

After

select {{ var("cols") }}, b
from {{ ref("orders") }}
where c = {{ var('d', 'x') }} and e in ({{ ['a', 'b'] | join(', ') }})

A frame breaks after its opener​

width 80

Before

{% set run_limits_query = var('snowplow__start_date', 'a_default_value_long_enough_that_this_cannot_join') -%}

select 1 as x

After

{%
set run_limits_query = var(
'snowplow__start_date',
'a_default_value_long_enough_that_this_cannot_join'
)
-%}

select 1 as x

A glued moustache adjacent tag still joins​

Before

{% if true %}
{{config(alias='customer_events_xa',
materialized="view")}}
select 1 as a
{% endif %}

After

{% if true %}
{{ config(alias = 'customer_events_xa', materialized = "view") }}
select 1 as a
{% endif %}

A glued reflow writes the line it anchored to​

Before

select
a,
b {{ ', x' if (var('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'y') | length > 1) }},
c
from t

After

select
a,
b
{{
', x'
if (var('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'y') | length > 1)
}},
c
from t

A keyword argument keeps its spaces​

Before

{{ config(materialized='table', schema='marts') }}
select a from t

After

{{ config(materialized = 'table', schema = 'marts') }}

select a
from t

A list in an elif condition formats like a set​

width 80

Before

{% set cols = ['user_id', 'updater_name'] %}

select
{% for col in cols %}
{% if col in ['user_id','organization_id'] %}
coalesce(u.{{ col }}, -1) as {{ col }},
{% elif col in ['updater_user_id', 'updater_organization_id', 'updater_last_login_at', 'updater_name'] %}
coalesce(u.{{ col }}, 'none') as {{ col }},
{% endif %}
{% endfor %}
u.id
from u

After

{% set cols = ['user_id', 'updater_name'] %}

select
{% for col in cols %}
{% if col in ['user_id', 'organization_id'] %}
coalesce(u.{{ col }}, -1) as {{ col }},
{%
elif col in [
'updater_user_id',
'updater_organization_id',
'updater_last_login_at',
'updater_name'
]
%}
coalesce(u.{{ col }}, 'none') as {{ col }},
{% endif %}
{% endfor %}
u.id
from u

A long loop head breaks before its filter​

Before

select
issue_id
{% for col in issue_columns if col.name|lower not in ['valid_starting_on','issue_id','issue_day_id','source_relation'] %}
, {{ col.name }}
{% endfor %}
from issues

After

select
issue_id
{%
for col in issue_columns
if col.name | lower not in ['valid_starting_on', 'issue_id', 'issue_day_id', 'source_relation']
%}
, {{ col.name }}
{% endfor %}
from issues

A macro definition body indents under its tags​

Before

{%- macro gid(prefix='', descriptor=[], study_id='') -%}
{{ prefix }}{{ study_id }}
{%- endmacro -%}

After

{%- macro gid(prefix='', descriptor=[], study_id='') -%}
{{ prefix }}{{ study_id }}
{%- endmacro -%}

A mid line frame closes at its own row​

width 60

Before

select a
from t
where
x = 1
and {{ first_operand_name_here ~ second_operand_name ~ third_operand_nm }}

After

select a
from t
where
x = 1
and {{
first_operand_name_here
~ second_operand_name
~ third_operand_nm
}}

A multi target set breaks like a call​

Before

{% set min_last_success,
max_last_success,
models_matched_from_manifest,
has_matched_all_models = var('snowplow__manifest_statuses', ['a_first_default_element',
'a_second_default_element',
'a_third_default_element',
'a_fourth_default_element']) -%}

select 1 as x

After

{%
set min_last_success, max_last_success, models_matched_from_manifest, has_matched_all_models = var(
'snowplow__manifest_statuses',
['a_first_default_element', 'a_second_default_element', 'a_third_default_element', 'a_fourth_default_element']
)
-%}

select 1 as x

A multiline string in a reflowed tag is still recognised​

Before

{%- macro dd(x, y, z) -%}datediff(second, col_a, col_b){%- endmacro -%}

select
{{ dd(
'first_request_time.time_of_first_request',
"least(
coalesce(first_request_time.time_of_first_requested_reviewer_review, " ~ 'now()' ~ "),
coalesce(issue.closed_at, " ~ 'now()' ~ "))",
'second')
}}/ 60/60 as hours_request_review_to_first_action
from t

After

{%- macro dd(x, y, z) -%}datediff(second, col_a, col_b){%- endmacro -%}

select
{{
dd(
'first_request_time.time_of_first_request',
"least(
coalesce(first_request_time.time_of_first_requested_reviewer_review, "
~ 'now()'
~ "),
coalesce(issue.closed_at, "
~ 'now()'
~ "))",
'second'
)
}}
/ 60
/ 60 as hours_request_review_to_first_action
from t

A set too wide for its head folds at its operator​

width 80

Before

{% macro reporting_window() %}
{%- set window_end_including_the_grace_period = the_configured_window_end + the_grace_period -%}
{% endmacro %}

After

{% macro reporting_window() %}
{%-
set window_end_including_the_grace_period = the_configured_window_end
+ the_grace_period
-%}
{% endmacro %}

A tag at the head of a name is not the body s bracket​

dialect snowflake · width 80

Before

CREATE OR REPLACE TABLE {{ database_name }}.synapse_raw.filesnapshot (ID NUMBER, NAME VARCHAR, IS_PUBLIC BOOLEAN)

After

CREATE OR REPLACE TABLE {{ database_name }}.synapse_raw.filesnapshot(
ID NUMBER,
NAME VARCHAR,
IS_PUBLIC BOOLEAN
)

A trailing comma after a sole bracket is not dropped​

Before

{{ config(materialized='table', indexes=[{'columns': ['dtm_id']},]) }}

select 1 as a

After

{{ config(materialized = 'table', indexes = [{'columns': ['dtm_id']},]) }}

select 1 as a

A trim marked set re breaks after its paren​

Before

{% set run_limits_query = var('snowplow__start_date',
'a_default_start_date_value_long_enough_that_this_tag_cannot_join_onto_one_line') -%}

select 1 as x

After

{%
set run_limits_query = var(
'snowplow__start_date',
'a_default_start_date_value_long_enough_that_this_tag_cannot_join_onto_one_line'
)
-%}

select 1 as x

A wrapped macro signature does not move the body​

Before

{% macro short_signature(model, column) -%}
{{ return(model ~ column) }}
{%- endmacro %}

{% macro wrapped_signature(
model, column="partitiondate", overlap_days=0, subtract_interval=none
) -%}
{{ return(model ~ column) }}
{%- endmacro %}

After

{% macro short_signature(model, column) -%}
{{ return(model ~ column) }}
{%- endmacro %}

{% macro wrapped_signature(
model, column="partitiondate", overlap_days=0, subtract_interval=none
) -%}
{{ return(model ~ column) }}
{%- endmacro %}

An author broken interior frames on its opener​

Before

with base as (
select
{{
var('staging_columns_for_the_issue_model',
'a_default_value_long_enough_that_the_tag_cannot_be_joined_onto_one_line')
}} as cols
from t
)
select a from base

After

with
base as (
select
{{
var(
'staging_columns_for_the_issue_model',
'a_default_value_long_enough_that_the_tag_cannot_be_joined_onto_one_line'
)
}} as cols
from t
)
select a
from base

An unspaced tag between whole tokens is formatted too​

Before

select {{var ("cols")}}, b
from {{ref("orders")}} join {{ref("items")}}_v using (id)
where c = '{{var("d")}}'

After

select {{ var("cols") }}, b from {{ ref("orders") }} join {{ref("items")}}_v using (id) where c = '{{var("d")}}'

Embedded jinja in string stays as written​

Before

{% set post_hooks = ['{{ expose_spells(\'["ethereum"]\', "sector", "labels") }}', 'grant select on {{ this }} to role reader'] %}

select * from src

After

{%
set post_hooks = [
'{{ expose_spells(\'["ethereum"]\', "sector", "labels") }}',
'grant select on {{ this }} to role reader'
]
%}

select *
from src

Fitting interior stays as written​

Before

select
{{ dbt_utils.generate_surrogate_key(['id', 'source']) }} as row_key,
loaded_at
from src

After

select {{ dbt_utils.generate_surrogate_key(['id', 'source']) }} as row_key, loaded_at from src

If condition breaks at its operators​

Before

select *
from t
{% if cutoff_date_tbl and cutoff_date_tbl.columns and cutoff_date_tbl.columns[0] and cutoff_date_tbl.columns[0][0] is not none and enable_cutoff_filtering %}
where created_at >= '{{ cutoff }}'
{% endif %}

After

select *
from t
{%
if
cutoff_date_tbl
and cutoff_date_tbl.columns
and cutoff_date_tbl.columns[0]
and cutoff_date_tbl.columns[0][0] is not none
and enable_cutoff_filtering
%}
where created_at >= '{{ cutoff }}'
{% endif %}

Overflowing call breaks inside its brackets​

Before

select
{{ dbt_utils.generate_surrogate_key(['feed_publisher_name', 'feed_publisher_url', 'feed_lang', 'default_lang', 'feed_version', 'feed_contact_email', 'feed_contact_url', 'feed_start_date', 'feed_end_date']) }} as feed_info_identifier,
feed_publisher_name
from feed_info

After

select
{{
dbt_utils.generate_surrogate_key([
'feed_publisher_name',
'feed_publisher_url',
'feed_lang',
'default_lang',
'feed_version',
'feed_contact_email',
'feed_contact_url',
'feed_start_date',
'feed_end_date'
])
}} as feed_info_identifier,
feed_publisher_name
from feed_info

Set assignment breaks inside its brackets​

Before

{% set excluded_columns = ['_fivetran_synced', 'email_domain_classification', 'marketing_consent_updated_at', 'notification_email_domain_classification', 'is_verified_email'] %}
select *
from users

After

{%
set excluded_columns = [
'_fivetran_synced',
'email_domain_classification',
'marketing_consent_updated_at',
'notification_email_domain_classification',
'is_verified_email'
]
%}

select *
from users