bito-code-review[bot] commented on code in PR #42735:
URL: https://github.com/apache/superset/pull/42735#discussion_r3709705398


##########
tests/unit_tests/charts/test_client_processing.py:
##########
@@ -2200,6 +2200,51 @@ def 
test_apply_client_processing_csv_format_escapes_formula_values():
     assert "\n=SUM(1+1)" not in processed["queries"][0]["data"]
 
 
+def test_apply_client_processing_csv_format_bytes_data():
+    """
+    Regression for #32370: "Export to pivoted .csv" fails with a 505 error.
+
+    For a real ``resultType=post_processed``/``resultFormat=csv`` request,
+    the query's ``data`` is CSV *bytes* by the time it reaches
+    ``apply_client_processing`` --
+    ``QueryContextProcessor.get_data`` encodes it to bytes for any CSV
+    result_format (``superset/common/query_context_processor.py``), and
+    that's the exact payload ``ChartDataRestApi._send_chart_response`` hands
+    to ``apply_client_processing`` for a POST_PROCESSED result. The CSV
+    branch here passes that straight into ``StringIO(data)``, which raises
+    ``TypeError: initial_value must be str or None, not bytes`` -- a crash
+    that reproduces for every pivoted CSV export, not only the reporter's
+    special-character metric labels (this test uses one anyway, to also
+    pin the originally reported symptom).
+    """
+
+    result = {
+        "queries": [
+            {
+                "result_format": ChartDataResultFormat.CSV,
+                "data": b"name,% of total\nA,1\nB,2\n",
+            }
+        ]
+    }
+    form_data = {
+        "datasource": "19__table",
+        "viz_type": "pivot_table_v2",
+        "slice_id": 69,
+        "groupbyColumns": [],
+        "groupbyRows": ["name"],
+        "metrics": ["% of total"],
+        "metricsLayout": "COLUMNS",
+        "aggregateFunction": "Sum",
+        "rowOrder": "key_a_to_z",
+        "colOrder": "key_a_to_z",
+        "result_format": "csv",
+        "result_type": "post_processed",
+    }
+
+    processed = apply_client_processing(result, form_data)
+    assert "% of total" in processed["queries"][0]["data"]

Review Comment:
   <!-- Bito Reply -->
   The suggestion provided by the reviewer is correct and addresses the 
identified regression. Applying the suggested bytes-decode guard ensures that 
the CSV data is properly converted to a string before being passed to 
`StringIO`, preventing the `TypeError` crash.
   
   **superset/charts/client_processing.py**
   ```
   if isinstance(data, bytes):
       data = data.decode("utf-8")
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to