codeant-ai-for-open-source[bot] commented on code in PR #41184:
URL: https://github.com/apache/superset/pull/41184#discussion_r3564137520
##########
superset-frontend/plugins/plugin-chart-pivot-table/src/PivotTableChart.tsx:
##########
@@ -341,22 +296,45 @@ export default function PivotTableChart(props:
PivotTableProps) {
const unpivotedData = useMemo(
() =>
- data.reduce(
- (acc: Record<string, any>[], record: Record<string, any>) => [
- ...acc,
- ...metricNames
+ // `data` is now one entry per rollup level. Tag every record with the
+ // row/column dimension labels of the level that produced it (mirroring
+ // the METRIC_KEY injection used for the full rows/cols below) so
+ // PivotData can slot each pre-computed value without re-aggregating.
+ // buildGroupbyCombinations already applied transposePivot, so the
level's
+ // groupby is display-oriented and is not transposed again here.
+ data.flatMap((query: QueryData) => {
+ let levelRows = query.groupby.rows.map(getColumnLabel);
+ let levelCols = query.groupby.columns.map(getColumnLabel);
+ if (metricsLayout === MetricsLayoutEnum.ROWS) {
+ levelRows = combineMetric
+ ? [...levelRows, METRIC_KEY]
+ : [METRIC_KEY, ...levelRows];
+ } else {
+ levelCols = combineMetric
+ ? [...levelCols, METRIC_KEY]
+ : [METRIC_KEY, ...levelCols];
+ }
+ return query.data.flatMap((record: Record<string, any>) =>
+ metricNames
.map((name: string) => ({
...record,
[METRIC_KEY]: name,
value: record[name],
// Mark currency column for per-cell currency detection in
aggregators
__currencyColumn: currencyCodeColumn,
+ // The level this record belongs to (used by PivotData
placement).
+ // Namespaced with a `__` prefix (like `__metricKey` below) so it
+ // can't collide with a real dataset column named
`rows`/`columns`.
+ __rows: levelRows,
+ __columns: levelCols,
+ // Identify the metric pseudo-dimension so PivotData can feed the
+ // metric-collapsed totals (the opposite "Total" axis + corner).
+ __metricKey: METRIC_KEY,
}))
- .filter(record => record.value !== null),
- ],
- [],
- ),
- [data, metricNames, currencyCodeColumn],
+ .filter(r => r.value !== null),
Review Comment:
**Suggestion:** Filtering out every record whose metric value is `null`
drops entire row/column keys for groups whose DB-computed value is null, so
those groups disappear from the pivot instead of rendering as blank cells. Keep
null-valued records and let the renderer/formatter display them as empty
values. [incorrect condition logic]
<details>
<summary><b>Severity Level:</b> Major ⚠️</summary>
```mdx
- ❌ Pivot tables omit groups with null metric values.
- ⚠️ Totals/subtotals ignore groups whose metrics are null.
- ⚠️ Users see incomplete data for non-additive metrics.
```
</details>
<details>
<summary><b>Steps of Reproduction ✅ </b></summary>
```mdx
1. The backend returns rollup-level data for the pivot table as
`QueryData[]`, where each
`QueryData` has `data: DataRecord[]` and `groupby` metadata
(`superset-frontend/plugins/plugin-chart-pivot-table/src/types.ts:61-68`).
`DataRecordValue` allows `null`, so a metric field (e.g. a ratio or CASE
expression) can
legitimately be `null` for a given group.
2. In the frontend `PivotTableChart` component
(`superset-frontend/plugins/plugin-chart-pivot-table/src/PivotTableChart.tsx:181-215`),
the `unpivotedData` array is built in a `useMemo` at lines 297-337. Each
backend row is
expanded per metric and tagged with `__rows`/`__columns`/`__metricKey` so
the pivot engine
can place it correctly. The final step `.filter(r => r.value !== null)` at
line 334 drops
every record whose metric value is `null`.
3. The resulting `unpivotedData` is passed to the `PivotTable` component
(`superset-frontend/plugins/plugin-chart-pivot-table/src/PivotTableChart.tsx:673-687`),
which is a thin wrapper around `TableRenderer`
(`react-pivottable/PivotTable.tsx:20-28`,
`react-pivottable/TableRenderers.tsx:67-80`). `TableRenderer` constructs a
`PivotData`
instance in `getBasePivotSettings` (`TableRenderers.tsx:105-112`), which
iterates over
`props.data` via `PivotData.forEachRecord`
(`react-pivottable/utilities.ts:216-224`) and
calls `processRecord` for each record.
4. `PivotData.processRecord` (`react-pivottable/utilities.ts:83-188`)
registers row and
column keys and creates total/body aggregators based solely on the records
it sees. If a
group (specific combination of `__rows` and `__columns`) only produced
`null` metric
values from the database, all of its records were removed by `.filter(r =>
r.value !==
null)` and `processRecord` is never invoked for that row/column key. As a
result,
`rowKeys`/`colKeys` do not contain that group, and the rendered table body
(`TableRenderers.tsx:188-191` iterating over `visibleRowKeys`) never shows
that row/column
at all, instead of displaying the group headers with blank/empty metric
cells. This
matches the suggestion: dropping null-valued records causes valid groups
(with DB-computed
null metrics) to disappear entirely from the pivot.
```
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=073f1561f80c4f27943b31ff437c0d02&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
[](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=073f1561f80c4f27943b31ff437c0d02&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-frontend/plugins/plugin-chart-pivot-table/src/PivotTableChart.tsx
**Line:** 334:334
**Comment:**
*Incorrect Condition Logic: Filtering out every record whose metric
value is `null` drops entire row/column keys for groups whose DB-computed value
is null, so those groups disappear from the pivot instead of rendering as blank
cells. Keep null-valued records and let the renderer/formatter display them as
empty values.
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%2F41184&comment_hash=f639b371313f79b171e9149fdcc8e1501e93aa9f5f05c9217f846171b1a5c1b6&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41184&comment_hash=f639b371313f79b171e9149fdcc8e1501e93aa9f5f05c9217f846171b1a5c1b6&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]