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


##########
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:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Test correct, production fix needed</b></div>
   <div id="fix">
   
   This regression test (lines 2203–2245) is correctly written and will 
correctly validate the production fix once the missing bytes-decode guard is 
added at `superset/charts/client_processing.py` line ~383. The crash 
(`TypeError: initial_value must be str or None, not bytes`) is confirmed: 
`QueryContextProcessor.get_data` encodes CSV output to bytes before passing it 
to `apply_client_processing`, and the CSV branch at line 383 passes it directly 
to `StringIO(data)` with no bytes check. Adding `if isinstance(data, bytes): 
data = data.decode("utf-8")` (after the existing str-strip at line 365, before 
the `pd.read_csv` call at line 382) will make the test pass.
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #85cd80</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



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