sha174n commented on code in PR #40392:
URL: https://github.com/apache/superset/pull/40392#discussion_r3321694381
##########
superset/connectors/sqla/models.py:
##########
@@ -867,6 +870,57 @@ def values_for_column(self, column_name: str, limit: int =
10000) -> list[Any]:
raise NotImplementedError()
+def validate_stored_expression(
+ database: Database,
+ catalog: str | None,
+ schema: str | None,
+ expression: str | None,
+) -> None:
+ """
+ Apply the adhoc-expression validator to a stored column or metric
expression.
+
+ Wrapping in a synthetic ``SELECT <expr>`` reuses the column-position parser
+ rules already enforced for adhoc expressions, so the same policy on
+ sub-queries, set operations, and multi-statement SQL applies to stored
+ expressions when they are saved.
+ """
+ if not expression:
+ return
+ engine = database.backend
+ wrapped = f"SELECT {expression}"
+
+ try:
+ parsed = SQLStatement(wrapped, engine)
+ except SupersetParseError as ex:
+ raise SupersetSecurityException(
+ SupersetError(
+ error_type=SupersetErrorType.ADHOC_SUBQUERY_NOT_ALLOWED_ERROR,
+ message=_(
+ "Custom SQL fields cannot be parsed as a single SQL
statement."
+ ),
+ level=ErrorLevel.ERROR,
+ )
+ ) from ex
Review Comment:
Fixed in e2931374f9: balanced Jinja blocks are now substituted with a `NULL`
placeholder before parsing, so templated expressions (`{{ current_username()
}}`, `{{ url_param(...) }}`) save successfully while the surrounding SQL is
still inspected for sub-queries, set operations, and multi-statements.
--
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]