Skip to content

Commit

Permalink
fix tconbeer#471: handle nested dictionary in jinja expression
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamin-awd committed Oct 19, 2023
1 parent 614f526 commit e4007ff
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/sqlfmt/rules/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@
Rule(
name="jinja_expression_end",
priority=610,
pattern=group(r"-?\}\}"),
pattern=group(r"-?\}\}(?<!\"\}\})"),
action=actions.raise_sqlfmt_bracket_error,
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-- source: https://github.com/tconbeer/sqlfmt/issues/471
{{
config(
materialized='incremental',
incremental_strategy='insert_overwrite',
external_location=generate_external_location("baz", "datalake/producer=foo/object=bar"),
partitioned_by=['batch_date'],
lf_tags_config={
'enabled': true,
'tags': {
'domain': 'foo',
'sensitivity': 'public'
}
}
)
}}
select
batch_date
from
{{ ref('bar') }}
{% if is_incremental() %}
where batch_date = '{{ var("ds") }}'
{% endif %}
)))))__SQLFMT_OUTPUT__(((((
-- source: https://github.com/tconbeer/sqlfmt/issues/471
{{
config(
materialized="incremental",
incremental_strategy="insert_overwrite",
external_location=generate_external_location("baz", "datalake/producer=foo/object=bar"),
partitioned_by=["batch_date"],
lf_tags_config={"enabled": true, "tags": {"domain": "foo", "sensitivity": "public"}},
)
}}
select batch_date
from {{ ref("bar") }}
{% if is_incremental() %} where batch_date = '{{ var("ds") }}' {% endif %}

11 changes: 11 additions & 0 deletions tests/unit_tests/test_nested_dictionary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from sqlfmt.api import format_string
from sqlfmt.mode import Mode
from tests.util import read_test_data


def test_handle_nested_dictionary_in_jinja_expression() -> None:
source_string, expected_string = read_test_data(
"unit_tests/test_nested_dictionary/test_nested_dictionary.sql"
)
actual = format_string(source_string, mode=Mode(line_length=100))
assert expected_string == actual

0 comments on commit e4007ff

Please sign in to comment.