From f3e68482a7dd35d3a21cacd5d28088d8f5d1dd92 Mon Sep 17 00:00:00 2001 From: "Hugh A. Miles II" Date: Wed, 3 Aug 2022 23:03:03 +0000 Subject: [PATCH] fix orderby on column types --- superset/models/helpers.py | 12 +++++++++++- superset/models/sql_lab.py | 11 +++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/superset/models/helpers.py b/superset/models/helpers.py index e0ba1f354255b..81dc53cfaee44 100644 --- a/superset/models/helpers.py +++ b/superset/models/helpers.py @@ -1298,6 +1298,12 @@ def get_timestamp_expression( time_expr = self.db_engine_spec.get_timestamp_expr(col, None, time_grain) return self.make_sqla_column_compatible(time_expr, label) + def get_sqla_col(self, col: Dict[str, Any]) -> Column: + label = col.get("column_name") + col_type = col.get("type") + col = sa.column(label, type_=col_type) + return self.make_sqla_column_compatible(col, label) + def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-many-branches,too-many-statements self, apply_fetch_values_predicate: bool = False, @@ -1434,7 +1440,11 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma col = metrics_exprs_by_expr.get(str(col), col) need_groupby = True elif col in columns_by_name: - col = columns_by_name[col].get_sqla_col() + gb_column_obj = columns_by_name[col] + if isinstance(gb_column_obj, dict): + col = self.get_sqla_col(gb_column_obj) + else: + col = gb_column_obj.get_sqla_col() elif col in metrics_exprs_by_label: col = metrics_exprs_by_label[col] need_groupby = True diff --git a/superset/models/sql_lab.py b/superset/models/sql_lab.py index a240f28d75dbb..de6ef78a0bd89 100644 --- a/superset/models/sql_lab.py +++ b/superset/models/sql_lab.py @@ -218,6 +218,16 @@ def columns(self) -> List[ResultSetColumnType]: @property def data(self) -> Dict[str, Any]: + order_by_choices = [] + for c in self.columns: + column_name = str(c.get("column_name") or "") + order_by_choices.append( + (json.dumps([column_name, True]), column_name + " [asc]") + ) + order_by_choices.append( + (json.dumps([column_name, False]), column_name + " [desc]") + ) + return { "time_grain_sqla": [ (g.duration, g.name) for g in self.database.grains() or [] @@ -231,6 +241,7 @@ def data(self) -> Dict[str, Any]: "sql": self.sql, "owners": self.owners_data, "database": {"id": self.database_id, "backend": self.database.backend}, + "order_by_choices": order_by_choices, } def raise_for_access(self) -> None: