Skip to content

Commit

Permalink
fix orderby on column types
Browse files Browse the repository at this point in the history
  • Loading branch information
hughhhh committed Aug 3, 2022
1 parent 16a4b25 commit f3e6848
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
12 changes: 11 additions & 1 deletion superset/models/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions superset/models/sql_lab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
Expand All @@ -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:
Expand Down

0 comments on commit f3e6848

Please sign in to comment.