rusackas commented on code in PR #40224:
URL: https://github.com/apache/superset/pull/40224#discussion_r3262199580
##########
tests/unit_tests/connectors/sqla/utils_test.py:
##########
@@ -137,3 +137,38 @@ 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.).
+ """
+ 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"}
+ ]
Review Comment:
Good catch — fixed in 8bb3082f. Captured the patched
`get_columns_description` mock and now assert that its `query` argument equals
`rendered_sql`. A regression where rendering is used for sqlglot parsing but
the raw SQL leaks downstream to the metadata call would now fail this assertion.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]