Skip to content

fix: render Jinja blocks before splitting on semicolons (#2650)#2782

Open
sfc-gh-moczko wants to merge 1 commit intomainfrom
fix/2650-jinja-block-semicolons
Open

fix: render Jinja blocks before splitting on semicolons (#2650)#2782
sfc-gh-moczko wants to merge 1 commit intomainfrom
fix/2650-jinja-block-semicolons

Conversation

@sfc-gh-moczko
Copy link
Collaborator

@sfc-gh-moczko sfc-gh-moczko commented Feb 24, 2026

Summary

  • Fixes SNOW-2397013: Jinja IF-statements are not interpreted correctly #2650: Jinja block statements ({% if %}, {% for %}, etc.) containing semicolons were broken because split_statements() splits on ; before Jinja rendering, producing invalid template fragments.
  • Moves Jinja rendering to a pre-render step on the whole content before split_statements(), while legacy (&{ }) and standard (<% %>) syntax continues to render per-statement (they are variable-only, no blocks).
  • Adds 7 new tests covering: if-blocks, for-loops, if/else, mixed Jinja+standard syntax, false conditions, file-based input, and undefined variable errors.

Root Cause

The SQL execution pipeline called split_statements() then per-statement template rendering. Since split_statements is unaware of Jinja block syntax, a query like:

{% if var == 'Jinja' %}select 1;{% endif %}

was split into two broken fragments:

  1. {% if var == 'Jinja' %}select 1; -- unclosed if block --> TemplateSyntaxError
  2. {% endif %} -- orphaned endif --> TemplateSyntaxError

Fix

Jinja block rendering now happens on the whole content before splitting. The per-statement pipeline still handles legacy/standard variable substitution (which never had block syntax). This is safe because Jinja {{ }}/{% %} syntax is orthogonal to <% %>/&{ } -- rendering one leaves the other untouched.

Changes

File Change
statement_reader.py Add pre_render parameter to query_reader() and files_reader()
manager.py Split Jinja into pre-render step (whole content) + per-statement step (legacy/standard only)
tests/sql/test_sql.py Add 7 test cases for Jinja block scenarios

Test plan

  • Reproduction test passes
  • For-loop test produces 3 statements
  • If/else test selects correct branch
  • Mixed Jinja + standard syntax works
  • False condition raises CliArgumentError (no statements to execute)
  • File-based Jinja blocks via -f flag work
  • Undefined variable in block raises clear error
  • All 83 tests in test_sql.py pass (including 10 existing parametrized syntax config tests)
  • All 33 test_statement_reader.py tests pass
  • All pre-commit hooks pass (ruff, black, mypy, codespell)
  • Live Snowflake smoke test: 3 scenarios pass end-to-end

`split_statements()` splits SQL on `;` before template rendering,
breaking Jinja block statements (`{% if %}`, `{% for %}`, etc.) that
contain semicolons. This moves Jinja rendering to a pre-render step
on the whole content before splitting, while legacy/standard syntax
continues to render per-statement.

Closes #2650

.... Generated with [Cortex Code](https://docs.snowflake.com/en/user-guide/cortex-code/cortex-code)

Co-Authored-By: Cortex Code <noreply@snowflake.com>
@sfc-gh-moczko sfc-gh-moczko requested a review from a team as a code owner February 24, 2026 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SNOW-2397013: Jinja IF-statements are not interpreted correctly

1 participant