diff --git a/superset/common/query_context.py b/superset/common/query_context.py index a7900cf6677c8..041100724c861 100644 --- a/superset/common/query_context.py +++ b/superset/common/query_context.py @@ -142,7 +142,7 @@ def get_query_result(self, query_object: QueryObject) -> Dict[str, Any]: def df_metrics_to_num(df: pd.DataFrame, query_object: QueryObject) -> None: """Converting metrics to numeric when pandas.read_sql cannot""" for col, dtype in df.dtypes.items(): - if dtype.type == np.object_ and col in query_object.metrics: + if dtype.type == np.object_ and col in query_object.metric_names: df[col] = pd.to_numeric(df[col], errors="coerce") def get_data(self, df: pd.DataFrame,) -> Union[str, List[Dict[str, Any]]]: @@ -166,6 +166,7 @@ def get_single_payload( if self.result_type == utils.ChartDataResultType.SAMPLES: row_limit = query_obj.row_limit or math.inf query_obj = copy.copy(query_obj) + query_obj.is_timeseries = False query_obj.orderby = [] query_obj.groupby = [] query_obj.metrics = [] diff --git a/superset/common/query_object.py b/superset/common/query_object.py index 43f7fee8ed110..cabbba69ab856 100644 --- a/superset/common/query_object.py +++ b/superset/common/query_object.py @@ -28,7 +28,7 @@ from superset.exceptions import QueryObjectValidationError from superset.typing import Metric from superset.utils import pandas_postprocessing -from superset.utils.core import DTTM_ALIAS, json_int_dttm_ser +from superset.utils.core import DTTM_ALIAS, get_metric_names, json_int_dttm_ser from superset.utils.date_parser import get_since_until, parse_human_timedelta from superset.views.utils import get_time_range_endpoints @@ -212,6 +212,10 @@ def __init__( ) self.extras[field.new_name] = value + @property + def metric_names(self) -> List[str]: + return get_metric_names(self.metrics) + def to_dict(self) -> Dict[str, Any]: query_object_dict = { "granularity": self.granularity, diff --git a/superset/connectors/sqla/models.py b/superset/connectors/sqla/models.py index 65c2a9f4dc683..d0887cd527da3 100644 --- a/superset/connectors/sqla/models.py +++ b/superset/connectors/sqla/models.py @@ -993,6 +993,7 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma time_range_endpoints = extras.get("time_range_endpoints") groupby_exprs_with_timestamp = OrderedDict(groupby_exprs_sans_timestamp.items()) + if granularity: dttm_col = columns_by_name[granularity] time_grain = extras.get("time_grain_sqla") @@ -1032,7 +1033,7 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma tbl = self.get_from_clause(template_processor) - if (is_sip_38 and metrics) or (not is_sip_38 and not columns): + if groupby_exprs_with_timestamp: qry = qry.group_by(*groupby_exprs_with_timestamp.values()) where_clause_and = []