codeant-ai-for-open-source[bot] commented on code in PR #40885:
URL: https://github.com/apache/superset/pull/40885#discussion_r3601316015


##########
superset/charts/client_processing.py:
##########
@@ -310,6 +310,33 @@ def table(
 }
 
 
+def _is_default_index_column(series: pd.Series) -> bool:
+    return series.tolist() == list(range(len(series)))
+
+
+def _read_excel_for_client_processing(
+    data: bytes,
+    form_data: dict[str, Any],
+) -> pd.DataFrame:
+    df = pd.read_excel(BytesIO(data))
+    if len(df.columns) == 0:
+        return df
+
+    first_column = df.columns[0]
+    expected_columns = {
+        *get_column_names(form_data.get("columns")),
+        *get_metric_names(form_data.get("metrics")),
+    }
+
+    if first_column in expected_columns:
+        return df
+
+    if _is_default_index_column(df.iloc[:, 0]):
+        return df.iloc[:, 1:].reset_index(drop=True)
+
+    return df.set_index(first_column)

Review Comment:
   **Suggestion:** The XLSX re-import logic infers expected columns only from 
`columns` and `metrics`, but pivot-table charts use 
`groupbyRows`/`groupbyColumns`; this causes real dimension columns to be 
misclassified as index columns and removed before post-processing, which can 
trigger missing-column failures or incorrect output. Build the expected-column 
set with the pivot-table field names as well (and only strip an index column 
when it is explicitly recognized as an export-added index). [incorrect 
condition logic]
   
   <details>
   <summary><b>Severity Level:</b> Critical 🚨</summary>
   
   ```mdx
   ❌ Pivot-table XLSX reports fail with missing-column errors.
   ❌ Excel attachments omit pivot dimensions or crash generation.
   ⚠️ XLSX exports inconsistent with CSV and JSON pivot output.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Create a chart with viz_type "pivot_table_v2" using non-empty groupbyRows 
/
   groupbyColumns and metrics, then schedule an XLSX email report so
   BaseReportState._get_notification_content() runs the tabular chart path
   (superset/commands/report/execute.py:26-68).
   
   2. When report_format is ReportDataFormat.XLSX and a chart is present,
   _get_notification_content() calls self._get_data(ChartDataResultFormat.XLSX)
   (superset/commands/report/execute.py:57-60), which builds a POST_PROCESSED 
request payload
   and sends it to ChartDataRestApi.get_data (verified in tests at
   tests/unit_tests/commands/report/execute_test.py:124-37).
   
   3. In ChartDataRestApi._send_chart_response() 
(superset/charts/data/api.py:11-18), when
   result_type == ChartDataResultType.POST_PROCESSED it invokes
   apply_client_processing(result, form_data, datasource), and for XLSX 
result_format
   apply_client_processing() (superset/charts/client_processing.py:81-137) 
passes the Excel
   bytes into _read_excel_for_client_processing(data, form_data) (lines 58-78).
   
   4. _read_excel_for_client_processing() builds expected_columns only from 
columns and
   metrics (superset/charts/client_processing.py:67-70), so for pivot_table_v2 
where
   dimensions live in groupbyRows/groupbyColumns the first dimension column is 
not in
   expected_columns, causing df.set_index(first_column) (line 78); 
pivot_table_v2() then
   calls pivot_df() with rows from groupbyRows (lines 40-55), but those row 
labels no longer
   exist as DataFrame columns, triggering a KeyError during df.pivot_table(...) 
and causing
   the XLSX report generation request to fail.
   ```
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=39fe7022c0db41e9a548b73cbb98b10c&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=39fe7022c0db41e9a548b73cbb98b10c&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
   
   *(Use Cmd/Ctrl + Click for best experience)*
   <details>
   <summary><b>Prompt for AI Agent 🤖 </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** superset/charts/client_processing.py
   **Line:** 326:337
   **Comment:**
        *Incorrect Condition Logic: The XLSX re-import logic infers expected 
columns only from `columns` and `metrics`, but pivot-table charts use 
`groupbyRows`/`groupbyColumns`; this causes real dimension columns to be 
misclassified as index columns and removed before post-processing, which can 
trigger missing-column failures or incorrect output. Build the expected-column 
set with the pivot-table field names as well (and only strip an index column 
when it is explicitly recognized as an export-added index).
   
   Validate the correctness of the flagged issue. If correct, How can I resolve 
this? If you propose a fix, implement it and please make it concise.
   Once fix is implemented, also check other comments on the same PR, and ask 
user if the user wants to fix the rest of the comments as well. if said yes, 
then fetch all the comments validate the correctness and implement a minimal fix
   ```
   </details>
   <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40885&comment_hash=efbfce6c8da73a95cbf1261fb8a0fb652f401ebbcec0e9200569b853d2d90e6a&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40885&comment_hash=efbfce6c8da73a95cbf1261fb8a0fb652f401ebbcec0e9200569b853d2d90e6a&reaction=dislike'>👎</a>



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