You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by jo...@apache.org on 2023/04/05 19:30:35 UTC

[superset] branch master updated: fix: Ensure the reporting framework handles charts with no data (#23585)

This is an automated email from the ASF dual-hosted git repository.

johnbodley pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/master by this push:
     new e8cfb31bff fix: Ensure the reporting framework handles charts with no data (#23585)
e8cfb31bff is described below

commit e8cfb31bff022389ca486f4b9c8a585b4e162ea1
Author: John Bodley <45...@users.noreply.github.com>
AuthorDate: Thu Apr 6 07:30:27 2023 +1200

    fix: Ensure the reporting framework handles charts with no data (#23585)
---
 superset/charts/post_processing.py              | 11 ++++++++---
 superset/utils/csv.py                           |  3 +++
 tests/unit_tests/charts/test_post_processing.py | 11 ++++++-----
 3 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/superset/charts/post_processing.py b/superset/charts/post_processing.py
index acfe95f391..fd10930db0 100644
--- a/superset/charts/post_processing.py
+++ b/superset/charts/post_processing.py
@@ -328,14 +328,19 @@ def apply_post_process(
         if query["result_format"] not in (rf.value for rf in ChartDataResultFormat):
             raise Exception(f"Result format {query['result_format']} not supported")
 
-        if not query["data"]:
+        data = query["data"]
+
+        if isinstance(data, str):
+            data = data.strip()
+
+        if not data:
             # do not try to process empty data
             continue
 
         if query["result_format"] == ChartDataResultFormat.JSON:
-            df = pd.DataFrame.from_dict(query["data"])
+            df = pd.DataFrame.from_dict(data)
         elif query["result_format"] == ChartDataResultFormat.CSV:
-            df = pd.read_csv(StringIO(query["data"]))
+            df = pd.read_csv(StringIO(data))
 
         # convert all columns to verbose (label) name
         if datasource:
diff --git a/superset/utils/csv.py b/superset/utils/csv.py
index 54f30362fe..a6c834b834 100644
--- a/superset/utils/csv.py
+++ b/superset/utils/csv.py
@@ -111,6 +111,9 @@ def get_chart_dataframe(
     pd.set_option("display.float_format", lambda x: str(x))
     df = pd.DataFrame.from_dict(result["result"][0]["data"])
 
+    if df.empty:
+        return None
+
     try:
         # if any column type is equal to 2, need to convert data into
         # datetime timestamp for that column.
diff --git a/tests/unit_tests/charts/test_post_processing.py b/tests/unit_tests/charts/test_post_processing.py
index be28aba922..84496bf1cf 100644
--- a/tests/unit_tests/charts/test_post_processing.py
+++ b/tests/unit_tests/charts/test_post_processing.py
@@ -18,9 +18,9 @@
 import json
 
 import pandas as pd
+import pytest
 from flask_babel import lazy_gettext as _
 from numpy import True_
-from pytest import raises
 from sqlalchemy.orm.session import Session
 
 from superset.charts.post_processing import apply_post_process, pivot_df, table
@@ -1389,7 +1389,7 @@ def test_apply_post_process_without_result_format():
     result = {"queries": [{"result_format": "foo"}]}
     form_data = {"viz_type": "pivot_table"}
 
-    with raises(Exception) as ex:
+    with pytest.raises(Exception) as ex:
         apply_post_process(result, form_data)
 
     assert ex.match("Result format foo not supported") == True
@@ -1659,12 +1659,13 @@ def test_apply_post_process_csv_format_empty_string():
     }
 
 
-def test_apply_post_process_csv_format_no_data():
+@pytest.mark.parametrize("data", [None, "", "\n"])
+def test_apply_post_process_csv_format_no_data(data):
     """
     It should be able to process csv results with no data
     """
 
-    result = {"queries": [{"result_format": ChartDataResultFormat.CSV, "data": None}]}
+    result = {"queries": [{"result_format": ChartDataResultFormat.CSV, "data": data}]}
     form_data = {
         "datasource": "19__table",
         "viz_type": "pivot_table_v2",
@@ -1726,7 +1727,7 @@ def test_apply_post_process_csv_format_no_data():
     }
 
     assert apply_post_process(result, form_data) == {
-        "queries": [{"result_format": ChartDataResultFormat.CSV, "data": None}]
+        "queries": [{"result_format": ChartDataResultFormat.CSV, "data": data}]
     }