Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Enforce the QueryResult.df to be a pandas.DataFrame object (Phase I) #8935

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions superset/connectors/druid/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -988,11 +988,9 @@ def values_for_column(self, column_name: str, limit: int = 10000) -> List:
def get_query_str(self, query_obj, phase=1, client=None):
return self.run_query(client=client, phase=phase, **query_obj)

def _add_filter_from_pre_query_data(
self, df: Optional[pd.DataFrame], dimensions, dim_filter
):
def _add_filter_from_pre_query_data(self, df: pd.DataFrame, dimensions, dim_filter):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type from client.export_pandas() is never None per here.

ret = dim_filter
if df is not None and not df.empty:
if not df.empty:
new_filters = []
for unused, row in df.iterrows():
fields = []
Expand Down Expand Up @@ -1379,7 +1377,7 @@ def query(self, query_obj: Dict) -> QueryResult:

if df is None or df.size == 0:
return QueryResult(
df=pd.DataFrame([]),
df=pd.DataFrame(),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is equivalent.

query=query_str,
duration=datetime.now() - qry_start_dttm,
)
Expand Down
4 changes: 2 additions & 2 deletions superset/connectors/sqla/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ class AnnotationDatasource(BaseDatasource):
cache_timeout = 0

def query(self, query_obj: Dict[str, Any]) -> QueryResult:
df = None
error_message = None
qry = db.session.query(Annotation)
qry = qry.filter(Annotation.layer_id == query_obj["filter"][0]["val"])
Expand All @@ -97,6 +96,7 @@ def query(self, query_obj: Dict[str, Any]) -> QueryResult:
try:
df = pd.read_sql_query(qry.statement, db.engine)
except Exception as e:
df = pd.DataFrame()
status = utils.QueryStatus.FAILED
logging.exception(e)
error_message = utils.error_msg_from_exception(e)
Expand Down Expand Up @@ -995,7 +995,7 @@ def mutator(df: pd.DataFrame) -> None:
try:
df = self.database.get_df(sql, self.schema, mutator)
except Exception as e:
df = None
df = pd.DataFrame()
status = utils.QueryStatus.FAILED
logging.exception(f"Query {sql} on schema {self.schema} failed")
db_engine_spec = self.database.db_engine_spec
Expand Down
11 changes: 6 additions & 5 deletions superset/models/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
# isort and pylint disagree, isort should win
# pylint: disable=ungrouped-imports
import humanize
import pandas as pd
import sqlalchemy as sa
import yaml
from flask import escape, g, Markup
Expand Down Expand Up @@ -368,11 +369,11 @@ class QueryResult: # pylint: disable=too-few-public-methods
def __init__( # pylint: disable=too-many-arguments
self, df, query, duration, status=QueryStatus.SUCCESS, error_message=None
):
self.df = df # pylint: disable=invalid-name
self.query = query
self.duration = duration
self.status = status
self.error_message = error_message
self.df: pd.DataFrame = df # pylint: disable=invalid-name
self.query: str = query
self.duration: int = duration
self.status: str = status
self.error_message: Optional[str] = error_message


class ExtraJSONMixin:
Expand Down
Loading