tconbeer/sqlfmt

the expression of jinja has indent bug.

Closed this issue · 2 comments

Describe the bug
There is a bug where if you use a multi-line string to indent an expression in a jinja block, the indentation will increase each time the format is updated.

To Reproduce

The indentation of expressions within blocks in jinja is added each time it is formatted.
Every time you change the select statement and need to reformat it, the indentation increases.

{{
    config(
        materialized="incremental",
        pre_hook="""
                delete from
                    dwh.user as t using (
                    select distinct campaign_name, date
                    from datalake.conversion
                    where date_part = date('{{ execution_date }}')
                ) as s
                where
                    t.campaign_name = s.campaign_name
                    and to_date(t.imported_at) <= s.date_part
                """,
    )
}}

select
    campaign_name,
    date_part,
    count(distinct user_id) as users

Expected behavior
Indentation does not increase after formatting.

{{
    config(
        materialized="incremental",
        pre_hook="""
                delete from
                    dwh.user as t using (
                    select distinct campaign_name, date
                    from datalake.conversion
                    where date_part = date('{{ execution_date }}')
                ) as s
                where
                    t.campaign_name = s.campaign_name
                    and to_date(t.imported_at) <= s.date_part
                """,
    )
}}

select campaign_name, date_part, count(distinct user_id) as users

Actual behavior
Provide any output generated by sqlfmt here. If this is a formatting issue, include the code generated by sqlfmt.

{{
    config(
        materialized="incremental",
        pre_hook="""
                delete from
                    dwh.user as t using (
                    select distinct campaign_name, date
                    from datalake.conversion
                    where date_part = date('{{ execution_date }}')
                ) as s
                where
                    t.campaign_name = s.campaign_name
                    and to_date(t.imported_at) <= s.date_part
                """,
    )
}}

select campaign_name, date_part, count(distinct user_id) as users

Additional context

$ sqlfmt --version
sqlfmt, version 0.21.0
shandy-sqlfmt.mov

Oof. Thanks for the report, this sucks.

I just reproduced on sqlfmt.com - it's specifically an issue with jinjafmt, this doesn't happen if you don't have black installed or run sqlfmt with the --no-jinjafmt option. A --fmt: off comment above the jinja block also stops it.

The patch for this issue has been released in v0.21.1