Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unterminated Jinja block...Expected end tag: ({% endfor %}) #326

Closed
ETG-msimons opened this issue Nov 22, 2022 · 1 comment · Fixed by #329
Closed

Unterminated Jinja block...Expected end tag: ({% endfor %}) #326

ETG-msimons opened this issue Nov 22, 2022 · 1 comment · Fixed by #329
Labels
bug Something isn't working

Comments

@ETG-msimons
Copy link

Describe the bug
sqlfmt encountered an error: Encountered unterminated Jinja block at position 720. Expected end tag: ({% endfor %})

To Reproduce
Steps to reproduce the behavior. If this is a formatting issue, include the input code here.

{% macro get_unique_attributes(source_table, node_col) %}

{% set attribute_query %} 
    select
        distinct x.key as attributes
    from {{ source_table }} x
    where startswith(x.key, '@')  -- attributes get parsed as keys that start with '@'
        and length(x.key) > 1 -- but keys of just '@' designate the node itself
        and regexp_count(x.path, '\\[') > 1 -- we don't need the attributes from the xml root node
        and not startswith(x.key, '@xmlns') -- we don't need attributed data about xml namespaces
{% endset %}

{% set results = run_query(attribute_query) %}

{% if execute %}
    {% set results_list = results.columns[0].values() %}
{% else %}
    {% set results_list = [] %}
{% endif %}

{% for attribute in results_list %}
    , get({{ node_col }}, '{{ attribute }}')::varchar(256) as attribute_{{ dbt_utils.slugify(attribute) | replace("@", "") }}
{% endfor %}

{% endmacro %}

Expected behavior
sqlfmt produces an error about an unterminated Jinja block and missing a endfor tag, but that tag does exist, and there doesn't appear to be any other incorrectly terminated items. The macro works without issue on Snowflake

Actual behavior

        1 file had errors while formatting.
        0 files left unchanged.
        dbt/macros/get_unique_attributes.sql
            sqlfmt encountered an error: Encountered unterminated Jinja block at position 720. Expected end tag: ({% endfor %})

Additional context
What is the output of sqlfmt --version?
Using VS Code extension, Trunk, which looks to be using [email protected]
The same error occurs when attempting the macro on the sqlfmt.com website.

@tconbeer
Copy link
Owner

Thanks for the report! I'll investigate next week. I think the issue is that we hit get() without first parsing select, since that's inside of a set block. GET is also Snowflake DDL that we don't yet support, so we skip ahead to the end of the query and miss the ending jinja tag

@tconbeer tconbeer added the bug Something isn't working label Nov 28, 2022
tconbeer added a commit that referenced this issue Nov 30, 2022
* fix #326: do not match unsupported ddl followed by open parens

* refactor: parse unsupported DDL more granularly instead of with one big DATA token

* chore: update changelog

* fix: catch SqlfmtSegmentError and handle appropriately, do not use finally; fixes issue where we could drop lines entirely

* fix: stop using tail recursion for create_segments_from_lines

* chore: update primer refs

* refactor: simplify create_segments, add tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants