Skip to content

sqlfmt fails to parse Jinja tags following on the same line as unsupported ddl #700

@oscep

Description

@oscep

Describe the bug
sqlfmt fails to format working Jinja macro.

To Reproduce
Create a clean virtual environment and install sqlfmt using pip install 'shandy-sqlfmt[jinjafmt]'.

Create a file named broken.sql with the content as follows:

{% macro athena_unload_table(database, schema, table, unload_params) %}
    {% if execute %}
        UNLOAD (SELECT * FROM "{{ database }}".{{ schema }}."{{ table }}")
        TO '{{ s3_path }}'
        WITH (
          format = '{{ format }}'
          {% if compression %}, compression = '{{ compression }}' {% endif %}
        );
    {% endif %}
{% endmacro %}

Execute sqlfmt broken.sql in your terminal.

Expected behavior
We expect sqlfmt to format the macro without errors, as the Jinja control flow is valid and properly closed.

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

sqlfmt broken.sql
1 file had errors while formatting.
0 files left unchanged.
broken.sql
    sqlfmt encountered an error: Closing jinja tag '{% endmacro %}' found at pos 349 does not match last opened tag '{% if execute %}' found at pos 72.

Additional context
What is the output of sqlfmt --version?

sqlfmt, version 0.27.0

What is the output of pip list (or pipx list if you installed using pipx)?

Package         Version
--------------- -------
black           25.1.0
click           8.2.1
Jinja2          3.1.6
MarkupSafe      3.0.2
mypy_extensions 1.1.0
packaging       25.0
pathspec        0.12.1
pip             24.0
platformdirs    4.3.8
shandy-sqlfmt   0.27.0
tqdm            4.67.1

[notice] A new release of pip is available: 24.0 -> 25.1.1
[notice] To update, run: pip install --upgrade pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions