Skip to main content

Indentation

How a block's body sits one level inside its tags, accumulating through nested blocks.

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

A set blocks tags share a column at the top level​

dialect snowflake

Left unchanged — already in dfmt's shape:

{%- set a -%}
xcol
{%- endset -%}

select 1

A set blocks tags share a column inside a branch​

dialect snowflake

Before

{% if flag -%}
{%- set a -%}
xcol
{%- endset -%}
select 1
{%- endif %}

After

{% if flag -%}
{%- set a -%}
xcol
{%- endset -%}
select 1
{%- endif %}

After an empty block code returns to its own column​

Left unchanged — already in dfmt's shape:

select
client_key,
min(session_start_timestamp) as first_seen_timestamp,
sum(sum_event_value_in_usd) as sum_event_value_in_usd,
count(distinct session_key) as count_sessions
{% if events %}
, sum(count_extra) as count_extra
{% endif %}
from sessions
group by 1

An extending guard opens the cte where​

Before

with
src as (
select *
from t
where a = 1
{% if flag %}
and b = 2
{% endif %}
)
select * from src

After

with
src as (
select *
from t
where
a = 1
{% if flag %}
and b = 2
{% endif %}
)
select *
from src

Block in a cte list opens its own line​

Before

with {% if flag %}
lookback as (select max(year * 4 + quarter) - 1 as min_yq from t),
{% endif %}
max_perim_year as (select max(year) as y from u)
select * from max_perim_year

After

with
{% if flag %}
lookback as (select max(year * 4 + quarter) - 1 as min_yq from t),
{% endif %}

max_perim_year as (select max(year) as y from u)
select *
from max_perim_year

Body nested under tags​

Before

select a
from t
{% if flag %}
where x = 1
{% endif %}

After

select a
from t
{% if flag %}
where x = 1
{% endif %}

Empty block keeps the authors column​

Left unchanged — already in dfmt's shape:

select a
from t1
where
x = 1
{% if cond %}
and ts >= (select max(ts) from tt)
{% endif %}

Guarded predicate stays with its siblings​

Before

select a
from t
where
true
and b is not null
{% if is_incremental() %}
and day >= date('{{ start }}')
{% endif %}

After

select a
from t
where
true
and b is not null
{% if is_incremental() %}
and day >= date('{{ start }}')
{% endif %}

Nesting accumulates​

Before

select a
from t
{% for src in sources %}
{% if flag %}
where kind = '{{ src }}'
{% endif %}
{% endfor %}

After

select a
from t
{% for src in sources %}
{% if flag %}
where kind = '{{ src }}'
{% endif %}
{% endfor %}

Sibling guards share the clause column​

Before

select t.id, p.price
from transactions t
left join prices p
on p.minute_bucket_at_exchange_time = t.block_time_minute_bucket_normalized
and p.symbol_normalized_for_matching = 'MATIC' and p.blockchain_name = 'polygon'
{% if not is_incremental() %}
and p.minute >= '2020-01-01'
{% endif %}
{% if is_incremental() %}
and p.minute >= now()
{% endif %}
left join dex_prices dp
on dp.hour = t.block_time
and dp.blockchain = 'polygon'

After

select t.id, p.price
from transactions t
left join prices p
on p.minute_bucket_at_exchange_time = t.block_time_minute_bucket_normalized
and p.symbol_normalized_for_matching = 'MATIC'
and p.blockchain_name = 'polygon'
{% if not is_incremental() %}
and p.minute >= '2020-01-01'
{% endif %}
{% if is_incremental() %}
and p.minute >= now()
{% endif %}
left join dex_prices dp on dp.hour = t.block_time and dp.blockchain = 'polygon'