codeant-ai-for-open-source[bot] commented on code in PR #43718:
URL: https://github.com/apache/superset/pull/43718#discussion_r3894834233
##########
superset/charts/client_processing.py:
##########
@@ -87,8 +165,16 @@ def pivot_df( # pylint: disable=too-many-locals,
too-many-arguments, too-many-s
show_columns_total: bool = False,
apply_metrics_on_rows: bool = False,
metric_name_aggfunc: Optional[str] = None,
+ show_values_as: Optional[str] = None,
) -> pd.DataFrame:
metric_name = __("Total (%(aggfunc)s)", aggfunc=metric_name_aggfunc or
aggfunc)
+ percent_mode = (
+ show_values_as if show_values_as in SHOW_VALUES_AS_PERCENT_MODES else
None
+ )
Review Comment:
**Suggestion:** Percent modes are accepted for non-additive aggregates, so
Average, Median, and distinct-count exports divide leaf results whose sums are
not valid rollup totals. [api mismatch]
**Assessment:** ๐ `Major` ยท ๐ `Occurrence: Sometimes`
[](https://docs.codeant.ai/cli/resolve-pr-comments-skill)
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=ede9ffa97d00425e906ca222984cc675&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=ede9ffa97d00425e906ca222984cc675&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
<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:** 171:173
**Comment:**
*Api Mismatch: Percent modes are accepted for non-additive aggregates,
so Average, Median, and distinct-count exports divide leaf results whose sums
are not valid rollup totals.
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%2F43718&comment_hash=6b8dbf5ec890addbb2f3135106d09264ce3302e16c28be8fcd01868e46b02e99&reaction=like'>๐</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F43718&comment_hash=6b8dbf5ec890addbb2f3135106d09264ce3302e16c28be8fcd01868e46b02e99&reaction=dislike'>๐</a>
##########
superset/charts/client_processing.py:
##########
@@ -75,6 +84,75 @@ def get_column_key(label: tuple[str, ...], metrics:
list[str]) -> tuple[Any, ...
return tuple(parts)
+def _apply_show_values_as( # pylint: disable=too-many-arguments
+ df: pd.DataFrame,
+ mode: str,
+ axis: dict[str, int],
+ metrics: list[str],
+ combine_metrics: bool,
+ inserted_rows: list[Any],
+ inserted_columns: list[Any],
+) -> pd.DataFrame:
+ """
+ Express each cell as a fraction of its row, column, or grand total.
+
+ Mirrors the client's ``fractionOf`` aggregator in
+ ``plugin-chart-pivot-table/src/react-pivottable/utilities.ts``. Two details
+ it inherits from there:
+
+ - Denominators are summed over leaf cells only. Totals and subtotals
+ inserted into the frame are numerators like any other cell -- a "% of
+ row" grand total row reads ``column total / grand total``, not the sum of
+ the fractions above it.
+ - A total is summed within a single metric, so a cell is never divided by a
+ total that mixes in another metric. Cross-metric totals (whose metric
+ level holds a total label rather than a metric name) divide by the
+ denominator spanning every metric.
+
+ A zero denominator yields NaN (blank) rather than infinity, matching
+ ``pandas_postprocessing.pivot``'s ``show_values_as``.
+ """
+ numeric = df.apply(pd.to_numeric, errors="coerce").astype(float)
+ is_multi_index = isinstance(df.columns, pd.MultiIndex)
+ # `combine_metrics` has already moved the metric to the lowest column
level.
+ metric_level = df.columns.nlevels - 1 if combine_metrics and
is_multi_index else 0
+ metric_names = set(metrics)
+ metric_of_column = [
+ key if key in metric_names else None
+ for key in df.columns.get_level_values(metric_level)
+ ]
+ leaf_rows = ~df.index.isin(inserted_rows)
+ leaf_columns = ~df.columns.isin(inserted_columns)
+
+ result = numeric.copy()
+ for metric in dict.fromkeys(metric_of_column):
+ selection = np.array([column == metric for column in metric_of_column])
+ denominator_selection = (
+ selection if metric is not None else np.ones(len(selection),
dtype=bool)
+ )
+ block = numeric.loc[:, selection]
+ if mode == PERCENT_OF_TOTAL:
+ leaf = numeric.loc[leaf_rows, leaf_columns & denominator_selection]
+ grand_total = leaf.to_numpy().sum()
+ fraction = block / (np.nan if grand_total == 0 else grand_total)
+ else:
Review Comment:
**Suggestion:** Any missing metric cell makes `grand_total` NaN, so
percent-total exports render every cell blank instead of summing available
values. [possible bug]
**Assessment:** ๐ `Major` ยท ๐ `Occurrence: Sometimes`
[](https://docs.codeant.ai/cli/resolve-pr-comments-skill)
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=979f06fcb63c45eb9136a36341695796&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=979f06fcb63c45eb9136a36341695796&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
<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:** 135:138
**Comment:**
*Possible Bug: Any missing metric cell makes `grand_total` NaN, so
percent-total exports render every cell blank instead of summing available
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%2F43718&comment_hash=7f02df05d0e789fe7849e3e349dc237d196b6efda0433a5d4c4d05a46a77665e&reaction=like'>๐</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F43718&comment_hash=7f02df05d0e789fe7849e3e349dc237d196b6efda0433a5d4c4d05a46a77665e&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]