You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by el...@apache.org on 2026/10/13 01:26:53 UTC

(superset) branch hughhhh/pfm-6-explore-indicator updated: fix(datasets): find a Jinja block without a quadratic regex

This is an automated email from the ASF dual-hosted git repository.

eschutho pushed a commit to branch hughhhh/pfm-6-explore-indicator
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/hughhhh/pfm-6-explore-indicator by this push:
     new ad5bb47d4be fix(datasets): find a Jinja block without a quadratic regex
ad5bb47d4be is described below

commit ad5bb47d4be0b43c39220f8e5e6b0fad326e1a06
Author: Elizabeth Thompson <es...@gmail.com>
AuthorDate: Tue Oct 13 01:26:45 2026 +0000

    fix(datasets): find a Jinja block without a quadratic regex
    
    `contains_jinja` searched with `{{.*?}}|{%.*?%}|{#.*?#}`, whose lazy
    match is retried from every opening delimiter, so a long run of them
    with no closing one takes quadratic time -- 40 KB of `{{` took about
    10 seconds. A length cap would not do: `is_transform_active` runs this
    on stored transforms, which have no length bound, and capping it would
    let an over-long stored transform holding Jinja skip the Jinja refusal.
    Scan for each kind's first opening delimiter and a closing one after it
    with `str.find`, which answers the same on every input in linear time.
    
    Co-Authored-By: Claude Opus 5.5 <no...@anthropic.com>
---
 superset/connectors/sqla/partition_mapping.py      | 28 ++++++++++++++----
 .../connectors/sqla/partition_mapping_test.py      | 33 ++++++++++++++++++++++
 2 files changed, 56 insertions(+), 5 deletions(-)

diff --git a/superset/connectors/sqla/partition_mapping.py b/superset/connectors/sqla/partition_mapping.py
index 2f4a0a71fd9..123841f539b 100644
--- a/superset/connectors/sqla/partition_mapping.py
+++ b/superset/connectors/sqla/partition_mapping.py
@@ -204,9 +204,10 @@ MAX_TRANSFORM_LENGTH = 1024
 #: Matched with word boundaries so ``:values`` is not mistaken for it.
 VALUE_PLACEHOLDER_RE = re.compile(r":value\b")
 
-#: Balanced Jinja blocks. The probe would render these in a different context
-#: at a different time from the chart query, so they are rejected at save time.
-JINJA_BLOCK_RE = re.compile(r"\{\{.*?\}\}|\{%.*?%\}|\{#.*?#\}", re.DOTALL)
+#: The opening and closing delimiters of each kind of Jinja block. The probe
+#: would render these in a different context at a different time from the chart
+#: query, so a transform holding one is rejected at save time.
+JINJA_BLOCK_DELIMITERS = (("{{", "}}"), ("{%", "%}"), ("{#", "#}"))
 
 #: MySQL and MariaDB "executable comments": ``/*!50000 ... */`` and
 #: ``/*M!50000 ... */`` are comments to every other reader and *statement text*
@@ -652,8 +653,25 @@ def references_sequence_pseudo_column(statement: SQLStatement) -> bool:
 
 
 def contains_jinja(transform: str | None) -> bool:
-    """Whether the transform contains a balanced Jinja block."""
-    return bool(transform) and JINJA_BLOCK_RE.search(transform or "") is not None
+    """
+    Whether the transform contains a balanced Jinja block.
+
+    A block is an opening delimiter followed, anywhere later, by its closing
+    one -- the same thing ``{{.*?}}`` and its two siblings match under
+    ``re.DOTALL``. Scanned with `str.find` rather than that pattern, because a
+    lazy match retried from every opening delimiter is quadratic in a run of
+    them, and this runs on stored transforms with no length bound of its own:
+    `is_transform_active` reads whatever the column holds. The earliest opening
+    delimiter is the only one worth trying, since any closing delimiter after a
+    later one is also after it.
+    """
+    if not transform:
+        return False
+    for opening, closing in JINJA_BLOCK_DELIMITERS:
+        start = transform.find(opening)
+        if start != -1 and transform.find(closing, start + len(opening)) != -1:
+            return True
+    return False
 
 
 def parse_skeleton(transform: str, standin: str = _PARSE_STANDIN) -> str:
diff --git a/tests/unit_tests/connectors/sqla/partition_mapping_test.py b/tests/unit_tests/connectors/sqla/partition_mapping_test.py
index f98f43d50b1..2948d6fdb7d 100644
--- a/tests/unit_tests/connectors/sqla/partition_mapping_test.py
+++ b/tests/unit_tests/connectors/sqla/partition_mapping_test.py
@@ -695,6 +695,39 @@ def test_contains_jinja(transform: str, expected: bool) -> None:
     assert contains_jinja(transform) is expected
 
 
+def test_contains_jinja_answers_as_the_block_pattern_does() -> None:
+    """
+    The scan has to agree with the lazy, ``DOTALL`` pattern it stands in for
+    on every input, including overlapping and unclosed delimiters, so that no
+    stored transform changes verdict. Every string of up to seven characters
+    over the delimiter characters, a newline and a filler is checked.
+    """
+    import itertools
+    import re
+
+    pattern = re.compile(r"\{\{.*?\}\}|\{%.*?%\}|\{#.*?#\}", re.DOTALL)
+    for length in range(8):
+        for chars in itertools.product("{}%#\na", repeat=length):
+            text = "".join(chars)
+            assert contains_jinja(text) is (pattern.search(text) is not None), text
+
+
+@pytest.mark.parametrize("opening", ["{{", "{%", "{#"])
+def test_contains_jinja_is_linear_in_a_run_of_opening_delimiters(
+    opening: str,
+) -> None:
+    """
+    A run of opening delimiters with no closing one made the lazy pattern retry
+    from every position, which is quadratic. Stored transforms are not
+    length-bounded here, so a long run has to stay cheap.
+    """
+    import time
+
+    started = time.monotonic()
+    assert contains_jinja(opening * 200_000) is False
+    assert time.monotonic() - started < 1
+
+
 @pytest.mark.parametrize(
     ("transform", "expected"),
     [