tconbeer / sqlfmt

sqlfmt formats your dbt SQL files so you don't have to
https://sqlfmt.com
Apache License 2.0
373 stars 15 forks source link

the expression of jinja has indent bug. #536

Closed yassun7010 closed 7 months ago

yassun7010 commented 7 months ago

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

https://github.com/tconbeer/sqlfmt/assets/47286750/f5714604-09c8-4c9b-ae7f-ad09955f95e2

tconbeer commented 7 months ago

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.

tconbeer commented 7 months ago

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