From 43d2fcc42854a000e634e68927081d74a2c1ab4e Mon Sep 17 00:00:00 2001 From: hughhhh Date: Fri, 21 Apr 2023 12:48:46 -0400 Subject: [PATCH] fix x-axis issue for cpq --- superset/models/sql_lab.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/superset/models/sql_lab.py b/superset/models/sql_lab.py index d37ed440db862..b2f0c8c1ed2f1 100644 --- a/superset/models/sql_lab.py +++ b/superset/models/sql_lab.py @@ -41,6 +41,7 @@ ) from sqlalchemy.engine.url import URL from sqlalchemy.orm import backref, relationship +from sqlalchemy.sql.elements import ColumnElement, literal_column from superset import security_manager from superset.jinja_context import BaseTemplateProcessor, get_template_processor @@ -52,7 +53,7 @@ ) from superset.sql_parse import CtasMethod, ParsedQuery, Table from superset.sqllab.limiting_factor import LimitingFactor -from superset.utils.core import QueryStatus, user_label +from superset.utils.core import get_column_name, QueryStatus, user_label if TYPE_CHECKING: from superset.connectors.sqla.models import TableColumn @@ -329,6 +330,29 @@ def get_column(self, column_name: Optional[str]) -> Optional[Dict[str, Any]]: return col return None + def adhoc_column_to_sqla( + self, + col: "AdhocColumn", # type: ignore + force_type_check: bool = False, + template_processor: Optional[BaseTemplateProcessor] = None, + ) -> ColumnElement: + """ + Turn an adhoc column into a sqlalchemy column. + :param col: Adhoc column definition + :param template_processor: template_processor instance + :returns: The metric defined as a sqlalchemy column + :rtype: sqlalchemy.sql.column + """ + label = get_column_name(col) + expression = self._process_sql_expression( + expression=col["sqlExpression"], + database_id=self.database_id, + schema=self.schema, + template_processor=template_processor, + ) + sqla_column = literal_column(expression) + return self.make_sqla_column_compatible(sqla_column, label) + class SavedQuery(Model, AuditMixinNullable, ExtraJSONMixin, ImportExportMixin): """ORM model for SQL query"""