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

Conversation

john-bodley
Copy link
Member

@john-bodley john-bodley commented Jan 8, 2020

CATEGORY

Choose one

  • Bug Fix
  • Enhancement (new features, refinement)
  • Refactor
  • Add tests
  • Build / Development Environment
  • Documentation

SUMMARY

In the past we have discussed whether we should use an empty pandas.DataFrame object or None to represent no-data. Previously df attribute of the QueryResult class could either be None or a pandas.DataFrame object and was inconsistency defined, i.e., the Druid native connector used the former whereas the SQL connector used the later. This was problematic as the None type wasn't always handled, e.g. here and here would both throw an exception if df was None.

This PR ensures that the df attribute is always a pandas.DataFrame object which can be empty.

Additionally I’ve also added some basic typing to help enforce a non-optional pandas.DataFrame and also provided a short circuit when processing of data for the NVD3TimeSeriesViz class if the data-frame is empty.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TEST PLAN

CI.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Changes UI
  • Requires DB Migration.
  • Confirm DB Migration upgrade and downgrade tested.
  • Introduces new feature or API
  • Removes existing feature or API

REVIEWERS

to: @etr2460 @michellethomas @mistercrunch @serenajiang @villebro @willbarrett

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.

@john-bodley john-bodley changed the title [fix] Enforce the query result data to be pandas.DataFrame [fix] Enforce the QueryResult.df to be pandas.DataFrame object Jan 8, 2020
@@ -439,11 +439,7 @@ def get_df_payload(self, query_obj=None, **kwargs):
and self.status != utils.QueryStatus.FAILED
):
try:
cache_value = dict(
dttm=cached_dttm,
df=df if df is not None else None,
Copy link
Member Author

Choose a reason for hiding this comment

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

df if df is not None else None is merely df.

Copy link
Member

Choose a reason for hiding this comment

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

Very true.

Copy link
Member

Choose a reason for hiding this comment

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

image

@john-bodley john-bodley force-pushed the john-bodley--fix-empty-data-frame branch from 732f1d1 to aaba7da Compare January 8, 2020 06:24
@john-bodley john-bodley changed the title [fix] Enforce the QueryResult.df to be pandas.DataFrame object [fix] Enforce the QueryResult.df to be a pandas.DataFrame object Jan 8, 2020
@john-bodley john-bodley marked this pull request as ready for review January 8, 2020 16:21
Copy link
Member

@willbarrett willbarrett left a comment

Choose a reason for hiding this comment

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

Looks reasonable to me. I approve of greater consistency in return values.

@@ -439,11 +439,7 @@ def get_df_payload(self, query_obj=None, **kwargs):
and self.status != utils.QueryStatus.FAILED
):
try:
cache_value = dict(
dttm=cached_dttm,
df=df if df is not None else None,
Copy link
Member

Choose a reason for hiding this comment

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

Very true.

@@ -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.

@@ -1155,6 +1153,9 @@ def process_data(self, df, aggregate=False):
if fd.get("granularity") == "all":
raise Exception(_("Pick a time granularity for your time series"))

if df.empty:
Copy link
Member Author

Choose a reason for hiding this comment

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

Short circuiting as pivot_table will throw an exception if the pandas.DataFrame is empty.

Copy link
Member

@villebro villebro left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -77,6 +77,8 @@
"size",
]

VizData = Optional[Union[List[Any], Dict[Any, Any]]]
Copy link
Member

Choose a reason for hiding this comment

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

Nice, we should really leverage alias types more.

@@ -439,11 +439,7 @@ def get_df_payload(self, query_obj=None, **kwargs):
and self.status != utils.QueryStatus.FAILED
):
try:
cache_value = dict(
dttm=cached_dttm,
df=df if df is not None else None,
Copy link
Member

Choose a reason for hiding this comment

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

image

@john-bodley john-bodley merged commit 2d456e8 into apache:master Jan 8, 2020
@john-bodley john-bodley deleted the john-bodley--fix-empty-data-frame branch January 8, 2020 19:50
@john-bodley john-bodley changed the title [fix] Enforce the QueryResult.df to be a pandas.DataFrame object [fix] Enforce the QueryResult.df to be a pandas.DataFrame object (Phase I) Jan 11, 2020
@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 0.36.0 labels Feb 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels size/L 🚢 0.36.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants