Skip to content

Commit

Permalink
chore: remove duplicate _process_sql_expression (#30213)
Browse files Browse the repository at this point in the history
(cherry picked from commit cddf153)
  • Loading branch information
betodealmeida authored and sadpandajoe committed Sep 12, 2024
1 parent 860c9c0 commit 4a6dd94
Showing 1 changed file with 19 additions and 36 deletions.
55 changes: 19 additions & 36 deletions superset/connectors/sqla/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@
from superset.exceptions import (
ColumnNotFoundException,
DatasetInvalidPermissionEvaluationException,
QueryClauseValidationException,
QueryObjectValidationError,
SupersetErrorException,
SupersetErrorsException,
Expand All @@ -103,10 +102,9 @@
ExploreMixin,
ImportExportMixin,
QueryResult,
validate_adhoc_subquery,
)
from superset.models.slice import Slice
from superset.sql_parse import ParsedQuery, sanitize_clause, Table
from superset.sql_parse import ParsedQuery, Table
from superset.superset_typing import (
AdhocColumn,
AdhocMetric,
Expand Down Expand Up @@ -1136,27 +1134,6 @@ def data(self) -> dict[str, Any]:
)


def _process_sql_expression(
expression: str | None,
database_id: int,
schema: str,
template_processor: BaseTemplateProcessor | None = None,
) -> str | None:
if template_processor and expression:
expression = template_processor.process_template(expression)
if expression:
try:
expression = validate_adhoc_subquery(
expression,
database_id,
schema,
)
expression = sanitize_clause(expression)
except (QueryClauseValidationException, SupersetSecurityException) as ex:
raise QueryObjectValidationError(ex.message) from ex
return expression


class SqlaTable(
Model,
BaseDatasource,
Expand Down Expand Up @@ -1552,12 +1529,15 @@ def adhoc_metric_to_sqla(
sqla_column = column(column_name)
sqla_metric = self.sqla_aggregations[metric["aggregate"]](sqla_column)
elif expression_type == utils.AdhocMetricExpressionType.SQL:
expression = _process_sql_expression(
expression=metric["sqlExpression"],
database_id=self.database_id,
schema=self.schema,
template_processor=template_processor,
)
try:
expression = self._process_sql_expression(
expression=metric["sqlExpression"],
database_id=self.database_id,
schema=self.schema,
template_processor=template_processor,
)
except SupersetSecurityException as ex:
raise QueryObjectValidationError(ex.message) from ex
sqla_metric = literal_column(expression)
else:
raise QueryObjectValidationError("Adhoc metric expressionType is invalid")
Expand All @@ -1582,12 +1562,15 @@ def adhoc_column_to_sqla( # pylint: disable=too-many-locals
:rtype: sqlalchemy.sql.column
"""
label = utils.get_column_name(col)
expression = _process_sql_expression(
expression=col["sqlExpression"],
database_id=self.database_id,
schema=self.schema,
template_processor=template_processor,
)
try:
expression = self._process_sql_expression(
expression=col["sqlExpression"],
database_id=self.database_id,
schema=self.schema,
template_processor=template_processor,
)
except SupersetSecurityException as ex:
raise QueryObjectValidationError(ex.message) from ex
time_grain = col.get("timeGrain")
has_timegrain = col.get("columnType") == "BASE_AXIS" and time_grain
is_dttm = False
Expand Down

0 comments on commit 4a6dd94

Please sign in to comment.