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

rusackas pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/master by this push:
     new f8600471fad test(datasets): regression test for Jinja not rendered on 
sync columns (#25839) (#40224)
f8600471fad is described below

commit f8600471fadd749f0cb36342a9b6f66d5b1f6ed9
Author: Evan Rusackas <[email protected]>
AuthorDate: Wed May 20 11:46:36 2026 -0700

    test(datasets): regression test for Jinja not rendered on sync columns 
(#25839) (#40224)
    
    Co-authored-by: Claude Code <[email protected]>
---
 tests/unit_tests/connectors/sqla/utils_test.py | 48 ++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/tests/unit_tests/connectors/sqla/utils_test.py 
b/tests/unit_tests/connectors/sqla/utils_test.py
index ef17bd9b280..6c6f5e38926 100644
--- a/tests/unit_tests/connectors/sqla/utils_test.py
+++ b/tests/unit_tests/connectors/sqla/utils_test.py
@@ -137,3 +137,51 @@ def test_get_virtual_table_metadata_multiple(mocker: 
MockerFixture) -> None:
     with pytest.raises(SupersetSecurityException) as excinfo:
         get_virtual_table_metadata(dataset)
     assert str(excinfo.value) == "Only single queries supported"
+
+
+def test_get_virtual_table_metadata_renders_jinja(mocker: MockerFixture) -> 
None:
+    """Regression for #25839: Jinja templates in a virtual dataset's SQL must
+    be rendered via the template processor before SQL parsing. Otherwise the
+    raw Jinja tokens reach sqlglot and the parser rejects them as a syntax
+    error (the user-visible symptom is "Invalid SQL" when clicking
+    "SYNC COLUMNS FROM SOURCE" on a dataset that uses {{ from_dttm }} etc.).
+    """
+    mock_get_columns_description = mocker.patch(
+        "superset.connectors.sqla.utils.get_columns_description",
+        return_value=[{"name": "rendered_col", "type": "INTEGER"}],
+    )
+
+    raw_sql = "SELECT * FROM tbl WHERE ts > '{{ from_dttm }}'"
+    rendered_sql = "SELECT * FROM tbl WHERE ts > '2024-01-01 00:00:00'"
+
+    dataset = mocker.MagicMock(sql=raw_sql)
+    dataset.database.db_engine_spec.engine = "postgresql"
+    dataset.template_params_dict = {}
+    dataset.get_template_processor().process_template.return_value = 
rendered_sql
+
+    # If Jinja rendering is skipped, sqlglot tries to parse the raw {{ ... }}
+    # and raises SupersetGenericDBErrorException / SupersetParseError.
+    assert get_virtual_table_metadata(dataset) == [
+        {"name": "rendered_col", "type": "INTEGER"}
+    ]
+
+    # The template processor MUST have been called with the raw SQL (the
+    # whole point of the bug fix). A future regression that re-introduces
+    # the "Jinja not rendered" path would either skip this call or call it
+    # with the wrong input.
+    dataset.get_template_processor().process_template.assert_any_call(
+        raw_sql, **dataset.template_params_dict
+    )
+
+    # End-to-end guard: the rendered SQL must reach get_columns_description,
+    # not the raw Jinja string. A regression where rendering is used for
+    # parsing only and the raw SQL leaks downstream would pass the
+    # process_template assertion above but fail this one.
+    call_args = mock_get_columns_description.call_args
+    assert call_args is not None, "get_columns_description was never called"
+    passed_query = call_args.kwargs.get("query")
+    if passed_query is None and call_args.args:
+        passed_query = call_args.args[-1]
+    assert passed_query == rendered_sql, (
+        f"get_columns_description received unrendered SQL: {passed_query!r}"
+    )

Reply via email to