This is an automated email from the ASF dual-hosted git repository. lyndsi pushed a commit to branch lyndsi/sql-lab-new-explore-button-functionality-and-move-save-dataset-to-split-save-button in repository https://gitbox.apache.org/repos/asf/superset.git
commit 708805fc887872b5307e37ff0b090d31703cb334 Author: Hugh A. Miles II <[email protected]> AuthorDate: Tue Jun 14 15:44:39 2022 +0000 refactor --- superset/models/helpers.py | 52 +++++++++++++++++++++++++--------------------- superset/models/sql_lab.py | 22 +++++++++++++++----- superset/views/core.py | 17 ++------------- 3 files changed, 47 insertions(+), 44 deletions(-) diff --git a/superset/models/helpers.py b/superset/models/helpers.py index 9cfe56d551..8b39ab03e2 100644 --- a/superset/models/helpers.py +++ b/superset/models/helpers.py @@ -67,13 +67,12 @@ from superset.jinja_context import ( get_template_processor, ) from superset.sql_parse import ( - has_table_query, extract_table_references, + has_table_query, ParsedQuery, sanitize_clause, Table as TableName, ) - from superset.utils import core as utils VIRTUAL_TABLE_ALIAS = "virtual_table" @@ -626,9 +625,15 @@ from sqlalchemy import Column from sqlalchemy.sql.elements import ColumnElement, Label, literal_column from superset.exceptions import QueryObjectValidationError -from superset.superset_typing import AdhocMetric, Metric, OrderBy, QueryObjectDict +from superset.superset_typing import ( + AdhocMetric, + FilterValue, + FilterValues, + Metric, + OrderBy, + QueryObjectDict, +) from superset.utils import core as utils -from superset.superset_typing import FilterValue, FilterValues, QueryObjectDict # todo(hugh): centralize where this code lives @@ -663,10 +668,6 @@ class ExploreMixin: "MAX": sa.func.MAX, } - @property - def data(self): - return {"foo": "bar"} - @property def owners_data(self): return [] @@ -689,7 +690,7 @@ class ExploreMixin: @property def column_names(self): - return [col.get('column_name') for col in self.columns] + return [col.get("column_name") for col in self.columns] @property def offset(self): @@ -727,7 +728,7 @@ class ExploreMixin: sqla_col = sqla_col.label(label) sqla_col.key = label_expected return sqla_col - + def mutate_query_from_config(self, sql: str) -> str: """Apply config's SQL_QUERY_MUTATOR @@ -841,7 +842,7 @@ class ExploreMixin: query_str_ext = self.get_query_str_extended(qry) sql = query_str_ext.sql - print('*****' * 5) + print("*****" * 5) # sql = "select count(*) from flights" status = QueryStatus.SUCCESS @@ -1206,7 +1207,9 @@ class ExploreMixin: "time_column": granularity, "time_grain": time_grain, "to_dttm": to_dttm.isoformat() if to_dttm else None, - "table_columns": [col.get('column_name') for col in self.columns], # [col.column_name for col in self.columns], + "table_columns": [ + col.get("column_name") for col in self.columns + ], # [col.column_name for col in self.columns], "filter": filter, } columns = columns or [] @@ -1223,7 +1226,7 @@ class ExploreMixin: applied_template_filters: List[str] = [] template_kwargs["removed_filters"] = removed_filters template_kwargs["applied_filters"] = applied_template_filters - template_processor = None # self.get_template_processor(**template_kwargs) + template_processor = None # self.get_template_processor(**template_kwargs) db_engine_spec = self.db_engine_spec prequeries: List[str] = [] orderby = orderby or [] @@ -1235,7 +1238,8 @@ class ExploreMixin: granularity = self.main_dttm_col columns_by_name: Dict[str, TableColumn] = { - col.get('column_name'): col for col in self.columns # col.column_name: col for col in self.columns + col.get("column_name"): col + for col in self.columns # col.column_name: col for col in self.columns } metrics_by_name: Dict[str, SqlMetric] = {m.metric_name: m for m in self.metrics} @@ -1329,7 +1333,7 @@ class ExploreMixin: # dedup columns while preserving order columns = groupby or columns for selected in columns: - if isinstance(selected, str): + if isinstance(selected, str): # if groupby field/expr equals granularity field/expr if selected == granularity: table_col = columns_by_name[selected] @@ -1369,9 +1373,7 @@ class ExploreMixin: self.schema, ) if isinstance(columns_by_name[selected], dict): - select_exprs.append( - literal_column(f"({selected})") - ) + select_exprs.append(literal_column(f"({selected})")) else: select_exprs.append( columns_by_name[selected].get_sqla_col() @@ -1403,7 +1405,7 @@ class ExploreMixin: db_engine_spec.time_secondary_columns and self.main_dttm_col in self.dttm_cols and self.main_dttm_col != dttm_col.column_name - ): + ): pass # todo(hughhh): fix time filter # time_filters.append( @@ -1468,12 +1470,12 @@ class ExploreMixin: time_grain=filter_grain, template_processor=template_processor ) elif col_obj and isinstance(col_obj, dict): - sqla_col = sa.column(col_obj.get('column_name')) + sqla_col = sa.column(col_obj.get("column_name")) elif col_obj: sqla_col = col_obj.get_sqla_col() - + if col_obj and isinstance(col_obj, dict): - col_type = col_obj.get('type') + col_type = col_obj.get("type") else: col_type = col_obj.type if col_obj else None col_spec = db_engine_spec.get_column_spec( @@ -1488,7 +1490,9 @@ class ExploreMixin: if col_obj and isinstance(col_obj, dict): col_advanced_data_type = "" else: - col_advanced_data_type = col_obj.advanced_data_type if col_obj else "" + col_advanced_data_type = ( + col_obj.advanced_data_type if col_obj else "" + ) if col_spec and not col_advanced_data_type: target_generic_type = col_spec.generic_type @@ -1583,7 +1587,7 @@ class ExploreMixin: raise QueryObjectValidationError( _("Invalid filter operation type: %(op)s", op=op) ) - # todo(hugh): fix this + # todo(hugh): fix this # where_clause_and += self.get_sqla_row_level_filters(template_processor) if extras: where = extras.get("where") diff --git a/superset/models/sql_lab.py b/superset/models/sql_lab.py index b6e318c10e..308dae808c 100644 --- a/superset/models/sql_lab.py +++ b/superset/models/sql_lab.py @@ -50,9 +50,8 @@ from superset.models.tags import QueryUpdater from superset.sql_parse import CtasMethod, ParsedQuery, Table from superset.sqllab.limiting_factor import LimitingFactor from superset.superset_typing import ResultSetColumnType -from superset.utils.core import GenericDataType, QueryStatus, user_label +from superset.utils.core import QueryStatus, user_label -from superset.superset_typing import ResultSetColumnType class Query(Model, ExtraJSONMixin, ExploreMixin): """ORM model for SQL query @@ -174,6 +173,7 @@ class Query(Model, ExtraJSONMixin, ExploreMixin): def columns(self) -> List[ResultSetColumnType]: # todo(hughhh): move this logic into a base class from superset.utils.core import GenericDataType + bool_types = ("BOOL",) num_types = ( "DOUBLE", @@ -189,10 +189,10 @@ class Query(Model, ExtraJSONMixin, ExploreMixin): ) date_types = ("DATE", "TIME") str_types = ("VARCHAR", "STRING", "CHAR") - columns = [] + columns = [] for col in self.extra.get("columns", []): computed_column = {**col} - col_type = col.get('type') + col_type = col.get("type") if col_type and any(map(lambda t: t in col_type.upper(), str_types)): computed_column["type_generic"] = GenericDataType.STRING @@ -203,7 +203,7 @@ class Query(Model, ExtraJSONMixin, ExploreMixin): if col_type and any(map(lambda t: t in col_type.upper(), date_types)): computed_column["type_generic"] = GenericDataType.TEMPORAL - computed_column["column_name"] = col.get('name') + computed_column["column_name"] = col.get("name") computed_column["groupby"] = True columns.append(computed_column) return columns # type: ignore @@ -222,6 +222,18 @@ class Query(Model, ExtraJSONMixin, ExploreMixin): "database": {"id": self.database_id, "backend": self.database.backend}, } + @property + def data(self) -> Dict[str, Any]: + return { + "columns": self.columns, + "metrics": [], + "id": self.id, + "type": self.type, + "name": self.sql, + "owners": self.owners_data, + "database": {"id": self.database_id, "backend": self.database.backend}, + } + def raise_for_access(self) -> None: """ Raise an exception if the user cannot access the resource. diff --git a/superset/views/core.py b/superset/views/core.py index 42681edb11..13942aca82 100755 --- a/superset/views/core.py +++ b/superset/views/core.py @@ -783,8 +783,8 @@ class Superset(BaseSupersetView): # pylint: disable=too-many-public-methods # Handle SIP-68 Models or explore view # API will always use /explore/<datasource_type>/<int:datasource_id>/ to query # new models to power any viz in explore - datasource_id = request.args.get('datasource_id', datasource_id) - datasource_type = request.args.get('datasource_type', datasource_type) + datasource_id = request.args.get("datasource_id", datasource_id) + datasource_type = request.args.get("datasource_type", datasource_type) if datasource_id and datasource_type: # 1. Query datasource object by type and id @@ -873,19 +873,6 @@ class Superset(BaseSupersetView): # pylint: disable=too-many-public-methods except (SupersetException, SQLAlchemyError): datasource_data = dummy_datasource_data - columns: List[Dict[str, Any]] = [] - if datasource: - datasource_data["owners"] = datasource.owners_data - if isinstance(datasource, Query): - # todo(hughhh): set is_dttm + name -> column_name - # datasource_data["data"] = datasource.data - # move all this logic into the class for the property data - datasource_data["columns"] = datasource.columns - datasource_data["metrics"] = datasource.extra.get("metrics", []) - datasource_data["id"] = datasource_id - datasource_data["type"] = datasource_type - datasource_data["name"] = datasource.sql - bootstrap_data = { "can_add": slice_add_perm, "can_download": slice_download_perm,
